massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / core-js / modules / esnext.async-iterator.reduce.js
1 'use strict';
2 // https://github.com/tc39/proposal-iterator-helpers
3 var $ = require('../internals/export');
4 var global = require('../internals/global');
5 var call = require('../internals/function-call');
6 var aCallable = require('../internals/a-callable');
7 var anObject = require('../internals/an-object');
8 var getBuiltIn = require('../internals/get-built-in');
9
10 var Promise = getBuiltIn('Promise');
11 var TypeError = global.TypeError;
12
13 $({ target: 'AsyncIterator', proto: true, real: true }, {
14   reduce: function reduce(reducer /* , initialValue */) {
15     var iterator = anObject(this);
16     var next = aCallable(iterator.next);
17     var noInitial = arguments.length < 2;
18     var accumulator = noInitial ? undefined : arguments[1];
19     aCallable(reducer);
20
21     return new Promise(function (resolve, reject) {
22       var loop = function () {
23         try {
24           Promise.resolve(anObject(call(next, iterator))).then(function (step) {
25             try {
26               if (anObject(step).done) {
27                 noInitial ? reject(TypeError('Reduce of empty iterator with no initial value')) : resolve(accumulator);
28               } else {
29                 var value = step.value;
30                 if (noInitial) {
31                   noInitial = false;
32                   accumulator = value;
33                   loop();
34                 } else {
35                   Promise.resolve(reducer(accumulator, value)).then(function (result) {
36                     accumulator = result;
37                     loop();
38                   }, reject);
39                 }
40               }
41             } catch (err) { reject(err); }
42           }, reject);
43         } catch (error) { reject(error); }
44       };
45
46       loop();
47     });
48   }
49 });