You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.7 KiB

'use strict';
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 IS_PURE = require('../internals/is-pure');
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;
});
});
// `AsyncIterator.prototype.take` method
// https://github.com/tc39/proposal-async-iterator-helpers
$({ target: 'AsyncIterator', proto: true, real: true, forced: IS_PURE }, {
take: function take(limit) {
anObject(this);
var remaining = toPositiveInteger(notANaN(+limit));
return new AsyncIteratorProxy(getIteratorDirect(this), {
remaining: remaining
});
}
});