"use strict";

const castPromise = (object) =>
  object && typeof object.then === "function" ? object : Promise.resolve(object);

const runIterator = (iterator) =>
  new Promise((resolve, reject) => {
    const iterate = (next, value) => {
      let ret;
      try {
        ret = next ? iterator.next(value) : iterator.throw(value);
      }
      catch (error) {
        reject(error);
        return;
      }

      if (ret.done) {
        resolve(ret.value);
        return;
      }

      const promise = Array.isArray(ret.value) ?
        Promise.all(ret.value) :
        Promise.resolve(ret.value);
      promise.then(
        (value) => iterate(true, value),
        (error) => iterate(false, error)
      );
    };

    iterate(true);
  });

const run = (generator) => runIterator(generator());

const wrapRun = (generator) =>
  function () {
    return runIterator(generator.apply(this, arguments));
  };

const wrapCPS = (fn, options) =>
  function () {
    const args = Array.from(arguments);
    return new Promise((resolve, reject) => {
      args.push(function () {
        const resultArgs = Array.from(arguments);
        const error = options && options.noError ? null : resultArgs.shift();

        if (error) reject(error);
        else if (options && options.multi) {
          const result = {};
          for (let i = 0; i < options.multi.length; i++) {
            result[options.multi[i]] = resultArgs[i];
          }
          resolve(result);
        }
        else resolve(resultArgs[0]);
      });
      fn.apply(null, args);
    });
  };

module.exports = {
  wrapCPS,
  run,
  wrapRun,
  castPromise,
};

@@ -12,123 +12,8 @@
 %22;%0A%0A
-const castPromise = (object) =%3E%0A  object && typeof object.then === %22function%22 ? object : Promise.resolve(object);%0A%0A
 cons
@@ -1498,22 +1498,7 @@
 un,%0A
-  castPromise,%0A
 %7D;%0A

Remove castPromise from promise-util