1 | 'use strict';
|
---|
2 | var $ = require('../internals/export');
|
---|
3 | var call = require('../internals/function-call');
|
---|
4 | var anObject = require('../internals/an-object');
|
---|
5 | var getIteratorDirect = require('../internals/get-iterator-direct');
|
---|
6 | var notANaN = require('../internals/not-a-nan');
|
---|
7 | var toPositiveInteger = require('../internals/to-positive-integer');
|
---|
8 | var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
|
---|
9 | var createIterResultObject = require('../internals/create-iter-result-object');
|
---|
10 | var IS_PURE = require('../internals/is-pure');
|
---|
11 |
|
---|
12 | var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) {
|
---|
13 | var state = this;
|
---|
14 |
|
---|
15 | return new Promise(function (resolve, reject) {
|
---|
16 | var doneAndReject = function (error) {
|
---|
17 | state.done = true;
|
---|
18 | reject(error);
|
---|
19 | };
|
---|
20 |
|
---|
21 | var loop = function () {
|
---|
22 | try {
|
---|
23 | Promise.resolve(anObject(call(state.next, state.iterator))).then(function (step) {
|
---|
24 | try {
|
---|
25 | if (anObject(step).done) {
|
---|
26 | state.done = true;
|
---|
27 | resolve(createIterResultObject(undefined, true));
|
---|
28 | } else if (state.remaining) {
|
---|
29 | state.remaining--;
|
---|
30 | loop();
|
---|
31 | } else resolve(createIterResultObject(step.value, false));
|
---|
32 | } catch (err) { doneAndReject(err); }
|
---|
33 | }, doneAndReject);
|
---|
34 | } catch (error) { doneAndReject(error); }
|
---|
35 | };
|
---|
36 |
|
---|
37 | loop();
|
---|
38 | });
|
---|
39 | });
|
---|
40 |
|
---|
41 | // `AsyncIterator.prototype.drop` method
|
---|
42 | // https://github.com/tc39/proposal-async-iterator-helpers
|
---|
43 | $({ target: 'AsyncIterator', proto: true, real: true, forced: IS_PURE }, {
|
---|
44 | drop: function drop(limit) {
|
---|
45 | anObject(this);
|
---|
46 | var remaining = toPositiveInteger(notANaN(+limit));
|
---|
47 | return new AsyncIteratorProxy(getIteratorDirect(this), {
|
---|
48 | remaining: remaining
|
---|
49 | });
|
---|
50 | }
|
---|
51 | });
|
---|