source: trip-planner-front/node_modules/core-js/internals/async-iterator-iteration.js@ 8d391a1

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: 2.4 KB
Line 
1'use strict';
2// https://github.com/tc39/proposal-iterator-helpers
3var aFunction = require('../internals/a-function');
4var anObject = require('../internals/an-object');
5var getBuiltIn = require('../internals/get-built-in');
6
7var Promise = getBuiltIn('Promise');
8var push = [].push;
9
10var createMethod = function (TYPE) {
11 var IS_TO_ARRAY = TYPE == 0;
12 var IS_FOR_EACH = TYPE == 1;
13 var IS_EVERY = TYPE == 2;
14 var IS_SOME = TYPE == 3;
15 return function (iterator, fn) {
16 anObject(iterator);
17 var next = aFunction(iterator.next);
18 var array = IS_TO_ARRAY ? [] : undefined;
19 if (!IS_TO_ARRAY) aFunction(fn);
20
21 return new Promise(function (resolve, reject) {
22 var closeIteration = function (method, argument) {
23 try {
24 var returnMethod = iterator['return'];
25 if (returnMethod !== undefined) {
26 return Promise.resolve(returnMethod.call(iterator)).then(function () {
27 method(argument);
28 }, function (error) {
29 reject(error);
30 });
31 }
32 } catch (error2) {
33 return reject(error2);
34 } method(argument);
35 };
36
37 var onError = function (error) {
38 closeIteration(reject, error);
39 };
40
41 var loop = function () {
42 try {
43 Promise.resolve(anObject(next.call(iterator))).then(function (step) {
44 try {
45 if (anObject(step).done) {
46 resolve(IS_TO_ARRAY ? array : IS_SOME ? false : IS_EVERY || undefined);
47 } else {
48 var value = step.value;
49 if (IS_TO_ARRAY) {
50 push.call(array, value);
51 loop();
52 } else {
53 Promise.resolve(fn(value)).then(function (result) {
54 if (IS_FOR_EACH) {
55 loop();
56 } else if (IS_EVERY) {
57 result ? loop() : closeIteration(resolve, false);
58 } else {
59 result ? closeIteration(resolve, IS_SOME || value) : loop();
60 }
61 }, onError);
62 }
63 }
64 } catch (error) { onError(error); }
65 }, onError);
66 } catch (error2) { onError(error2); }
67 };
68
69 loop();
70 });
71 };
72};
73
74module.exports = {
75 toArray: createMethod(0),
76 forEach: createMethod(1),
77 every: createMethod(2),
78 some: createMethod(3),
79 find: createMethod(4)
80};
Note: See TracBrowser for help on using the repository browser.