Last change
on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.5 KB
|
Line | |
---|
1 | 'use strict';
|
---|
2 | // https://github.com/tc39/proposal-iterator-helpers
|
---|
3 | var $ = require('../internals/export');
|
---|
4 | var aFunction = require('../internals/a-function');
|
---|
5 | var anObject = require('../internals/an-object');
|
---|
6 | var getBuiltIn = require('../internals/get-built-in');
|
---|
7 |
|
---|
8 | var Promise = getBuiltIn('Promise');
|
---|
9 |
|
---|
10 | $({ target: 'AsyncIterator', proto: true, real: true }, {
|
---|
11 | reduce: function reduce(reducer /* , initialValue */) {
|
---|
12 | var iterator = anObject(this);
|
---|
13 | var next = aFunction(iterator.next);
|
---|
14 | var noInitial = arguments.length < 2;
|
---|
15 | var accumulator = noInitial ? undefined : arguments[1];
|
---|
16 | aFunction(reducer);
|
---|
17 |
|
---|
18 | return new Promise(function (resolve, reject) {
|
---|
19 | var loop = function () {
|
---|
20 | try {
|
---|
21 | Promise.resolve(anObject(next.call(iterator))).then(function (step) {
|
---|
22 | try {
|
---|
23 | if (anObject(step).done) {
|
---|
24 | noInitial ? reject(TypeError('Reduce of empty iterator with no initial value')) : resolve(accumulator);
|
---|
25 | } else {
|
---|
26 | var value = step.value;
|
---|
27 | if (noInitial) {
|
---|
28 | noInitial = false;
|
---|
29 | accumulator = value;
|
---|
30 | loop();
|
---|
31 | } else {
|
---|
32 | Promise.resolve(reducer(accumulator, value)).then(function (result) {
|
---|
33 | accumulator = result;
|
---|
34 | loop();
|
---|
35 | }, reject);
|
---|
36 | }
|
---|
37 | }
|
---|
38 | } catch (err) { reject(err); }
|
---|
39 | }, reject);
|
---|
40 | } catch (error) { reject(error); }
|
---|
41 | };
|
---|
42 |
|
---|
43 | loop();
|
---|
44 | });
|
---|
45 | }
|
---|
46 | });
|
---|
Note:
See
TracBrowser
for help on using the repository browser.