Sha256: 215f52d7d8f265fb06e1629e02aa1538dd2cb7b066058eb44f940eaaac9a5507

Contents?: true

Size: 1.82 KB

Versions: 15

Compression:

Stored size: 1.82 KB

Contents

'use strict';
// https://github.com/tc39/proposal-iterator-helpers
var aFunction = require('../internals/a-function');
var anObject = require('../internals/an-object');
var getBuiltIn = require('../internals/get-built-in');

var Promise = getBuiltIn('Promise');
var push = [].push;

var createMethod = function (TYPE) {
  var IS_TO_ARRAY = TYPE == 0;
  var IS_FOR_EACH = TYPE == 1;
  var IS_EVERY = TYPE == 2;
  var IS_SOME = TYPE == 3;
  return function (iterator, fn) {
    anObject(iterator);
    var next = aFunction(iterator.next);
    var array = IS_TO_ARRAY ? [] : undefined;
    if (!IS_TO_ARRAY) aFunction(fn);

    return new Promise(function (resolve, reject) {
      var loop = function () {
        try {
          Promise.resolve(anObject(next.call(iterator))).then(function (step) {
            try {
              if (anObject(step).done) {
                resolve(IS_TO_ARRAY ? array : IS_SOME ? false : IS_EVERY || undefined);
              } else {
                var value = step.value;
                if (IS_TO_ARRAY) {
                  push.call(array, value);
                  loop();
                } else {
                  Promise.resolve(fn(value)).then(function (result) {
                    if (IS_FOR_EACH) {
                      loop();
                    } else if (IS_EVERY) {
                      result ? loop() : resolve(false);
                    } else {
                      result ? resolve(IS_SOME || value) : loop();
                    }
                  }, reject);
                }
              }
            } catch (err) { reject(err); }
          }, reject);
        } catch (error) { reject(error); }
      };

      loop();
    });
  };
};

module.exports = {
  toArray: createMethod(0),
  forEach: createMethod(1),
  every: createMethod(2),
  some: createMethod(3),
  find: createMethod(4)
};

Version data entries

15 entries across 15 versions & 5 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/core-js/internals/async-iterator-iteration.js
disco_app-0.18.0 test/dummy/node_modules/core-js/internals/async-iterator-iteration.js
disco_app-0.18.2 test/dummy/node_modules/core-js/internals/async-iterator-iteration.js
tang-0.2.1 spec/tang_app/node_modules/core-js/internals/async-iterator-iteration.js
tang-0.2.0 spec/tang_app/node_modules/core-js/internals/async-iterator-iteration.js
tang-0.1.0 spec/tang_app/node_modules/core-js/internals/async-iterator-iteration.js
tang-0.0.9 spec/tang_app/node_modules/core-js/internals/async-iterator-iteration.js
enju_library-0.3.8 spec/dummy/node_modules/core-js/internals/async-iterator-iteration.js
condenser-0.3 lib/condenser/processors/node_modules/core-js-pure/internals/async-iterator-iteration.js
condenser-0.2 lib/condenser/processors/node_modules/core-js-pure/internals/async-iterator-iteration.js
condenser-0.1 lib/condenser/processors/node_modules/core-js-pure/internals/async-iterator-iteration.js
condenser-0.0.12 lib/condenser/processors/node_modules/core-js-pure/internals/async-iterator-iteration.js
condenser-0.0.11 lib/condenser/processors/node_modules/core-js-pure/internals/async-iterator-iteration.js
condenser-0.0.10 lib/condenser/processors/node_modules/core-js-pure/internals/async-iterator-iteration.js
condenser-0.0.9 lib/condenser/processors/node_modules/core-js-pure/internals/async-iterator-iteration.js