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 | var iterator = state.iterator;
|
---|
15 | var returnMethod;
|
---|
16 |
|
---|
17 | if (!state.remaining--) {
|
---|
18 | var resultDone = createIterResultObject(undefined, true);
|
---|
19 | state.done = true;
|
---|
20 | returnMethod = iterator['return'];
|
---|
21 | if (returnMethod !== undefined) {
|
---|
22 | return Promise.resolve(call(returnMethod, iterator, undefined)).then(function () {
|
---|
23 | return resultDone;
|
---|
24 | });
|
---|
25 | }
|
---|
26 | return resultDone;
|
---|
27 | } return Promise.resolve(call(state.next, iterator)).then(function (step) {
|
---|
28 | if (anObject(step).done) {
|
---|
29 | state.done = true;
|
---|
30 | return createIterResultObject(undefined, true);
|
---|
31 | } return createIterResultObject(step.value, false);
|
---|
32 | }).then(null, function (error) {
|
---|
33 | state.done = true;
|
---|
34 | throw error;
|
---|
35 | });
|
---|
36 | });
|
---|
37 |
|
---|
38 | // `AsyncIterator.prototype.take` method
|
---|
39 | // https://github.com/tc39/proposal-async-iterator-helpers
|
---|
40 | $({ target: 'AsyncIterator', proto: true, real: true, forced: IS_PURE }, {
|
---|
41 | take: function take(limit) {
|
---|
42 | anObject(this);
|
---|
43 | var remaining = toPositiveInteger(notANaN(+limit));
|
---|
44 | return new AsyncIteratorProxy(getIteratorDirect(this), {
|
---|
45 | remaining: remaining
|
---|
46 | });
|
---|
47 | }
|
---|
48 | });
|
---|