Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.3 KB
|
Line | |
---|
1 | 'use strict';
|
---|
2 | // https://github.com/tc39/proposal-iterator-helpers
|
---|
3 | var $ = require('../internals/export');
|
---|
4 | var anObject = require('../internals/an-object');
|
---|
5 | var toPositiveInteger = require('../internals/to-positive-integer');
|
---|
6 | var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
|
---|
7 |
|
---|
8 | var AsyncIteratorProxy = createAsyncIteratorProxy(function (arg, Promise) {
|
---|
9 | var state = this;
|
---|
10 |
|
---|
11 | return new Promise(function (resolve, reject) {
|
---|
12 | var loop = function () {
|
---|
13 | try {
|
---|
14 | Promise.resolve(
|
---|
15 | anObject(state.next.call(state.iterator, state.remaining ? undefined : arg))
|
---|
16 | ).then(function (step) {
|
---|
17 | try {
|
---|
18 | if (anObject(step).done) {
|
---|
19 | state.done = true;
|
---|
20 | resolve({ done: true, value: undefined });
|
---|
21 | } else if (state.remaining) {
|
---|
22 | state.remaining--;
|
---|
23 | loop();
|
---|
24 | } else resolve({ done: false, value: step.value });
|
---|
25 | } catch (err) { reject(err); }
|
---|
26 | }, reject);
|
---|
27 | } catch (error) { reject(error); }
|
---|
28 | };
|
---|
29 |
|
---|
30 | loop();
|
---|
31 | });
|
---|
32 | });
|
---|
33 |
|
---|
34 | $({ target: 'AsyncIterator', proto: true, real: true }, {
|
---|
35 | drop: function drop(limit) {
|
---|
36 | return new AsyncIteratorProxy({
|
---|
37 | iterator: anObject(this),
|
---|
38 | remaining: toPositiveInteger(limit)
|
---|
39 | });
|
---|
40 | }
|
---|
41 | });
|
---|
Note:
See
TracBrowser
for help on using the repository browser.