1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- 'use strict';
- // https://github.com/tc39/proposal-iterator-helpers
- var $ = require('../internals/export');
- var call = require('../internals/function-call');
- var anObject = require('../internals/an-object');
- var getIteratorDirect = require('../internals/get-iterator-direct');
- var notANaN = require('../internals/not-a-nan');
- var toPositiveInteger = require('../internals/to-positive-integer');
- var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
- var createIterResultObject = require('../internals/create-iter-result-object');
- var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) {
- var state = this;
- var iterator = state.iterator;
- var returnMethod;
- if (!state.remaining--) {
- var resultDone = createIterResultObject(undefined, true);
- state.done = true;
- returnMethod = iterator['return'];
- if (returnMethod !== undefined) {
- return Promise.resolve(call(returnMethod, iterator, undefined)).then(function () {
- return resultDone;
- });
- }
- return resultDone;
- } return Promise.resolve(call(state.next, iterator)).then(function (step) {
- if (anObject(step).done) {
- state.done = true;
- return createIterResultObject(undefined, true);
- } return createIterResultObject(step.value, false);
- }).then(null, function (error) {
- state.done = true;
- throw error;
- });
- });
- $({ target: 'AsyncIterator', proto: true, real: true, forced: true }, {
- take: function take(limit) {
- return new AsyncIteratorProxy(getIteratorDirect(this), {
- remaining: toPositiveInteger(notANaN(+limit))
- });
- }
- });
|