"Promisify" a function, meaning to create a function which returns a promise for the value of the function once `this` and all arguments have been fulfilled.
960975
function promisify(f) {                              // given an underlying function,
  return function _promisify(...args) {              // return a function which
    return new Promise(                              // returns a promise
      resolve => Promise.all([this, ...args])        // which, when all args are resolved,
        .then(
          parms => resolve(f.call(...parms))         // resolves to the function result
        )
    );
  };
}
124974
function wrapFunctionToReturnPromise(fn, Promise) {
	return function() {
		try {
			const result = fn.apply(this, arguments);
			if (result instanceof Promise) return result;
			return Promise.resolve(result);
		} catch (err) {
			return new Promise((resolve, reject) => { // jshint ignore:line
				reject(err);
			});
		}
	};
}
182413
async function repeatablePromise (initialAssumptions, assumptionsFromResult, promiseGenerator) {
    let promise = promiseGenerator(initialAssumptions);
    let result = await promise;
    let finalAssumptions = assumptionsFromResult(result);
    if (JSON.stringify(initialAssumptions) === JSON.stringify(finalAssumptions)) {
        return promise;
    } else {
        return promiseGenerator(finalAssumptions);
    }
}
169667
function resolveValues(obj) {
    var ret = {};
    return Promise.all(Object.keys(obj).map(function (k) {
        var val = (0, _promisify2['default'])(obj[k]);
        return val.then(function (v) {
            return ret[k] = v;
        });
    })).then(function () {
        return ret;
    });
}
537714
function Promise(handler, parent) {
  var me = this;

  if (!(this instanceof Promise)) {
    throw new SyntaxError('Constructor must be called with the new operator');
  }

  if (typeof handler !== 'function') {
    throw new SyntaxError('Function parameter handler(resolve, reject) missing');
  }

  var _onSuccess = [];
  var _onFail = [];

  // status
  this.resolved = false;
  this.rejected = false;
  this.pending = true;

  /**
   * Process onSuccess and onFail callbacks: add them to the queue.
   * Once the promise is resolve, the function _promise is replace.
   * @param {Function} onSuccess
   * @param {Function} onFail
   * @private
   */
  var _process = function (onSuccess, onFail) {
    _onSuccess.push(onSuccess);
    _onFail.push(onFail);
  };

  /**
   * Add an onSuccess callback and optionally an onFail callback to the Promise
   * @param {Function} onSuccess
   * @param {Function} [onFail]
   * @returns {Promise} promise
   */
  this.then = function (onSuccess, onFail) {
    return new Promise(function (resolve, reject) {
      var s = onSuccess ? _then(onSuccess, resolve, reject) : resolve;
      var f = onFail    ? _then(onFail,    resolve, reject) : reject;

      _process(s, f);
    }, me);
  };

  /**
   * Resolve the promise
   * @param {*} result
   * @type {Function}
   */
  var _resolve = function (result) {
    // update status
    me.resolved = true;
    me.rejected = false;
    me.pending = false;

    _onSuccess.forEach(function (fn) {
      fn(result);
    });

    _process = function (onSuccess, onFail) {
      onSuccess(result);
    };

    _resolve = _reject = function () { };

    return me;
  };

  /**
   * Reject the promise
   * @param {Error} error
   * @type {Function}
   */
  var _reject = function (error) {
    // update status
    me.resolved = false;
    me.rejected = true;
    me.pending = false;

    _onFail.forEach(function (fn) {
      fn(error);
    });

    _process = function (onSuccess, onFail) {
      onFail(error);
    };

    _resolve = _reject = function () { }

    return me;
  };

  /**
   * Cancel te promise. This will reject the promise with a CancellationError
   * @returns {Promise} self
   */
  this.cancel = function () {
    if (parent) {
      parent.cancel();
    }
    else {
      _reject(new CancellationError());
    }

    return me;
  };

  /**
   * Set a timeout for the promise. If the promise is not resolved within
   * the time, the promise will be cancelled and a TimeoutError is thrown.
   * If the promise is resolved in time, the timeout is removed.
   * @param {number} delay     Delay in milliseconds
   * @returns {Promise} self
   */
  this.timeout = function (delay) {
    if (parent) {
      parent.timeout(delay);
    }
    else {
      var timer = setTimeout(function () {
        _reject(new TimeoutError('Promise timed out after ' + delay + ' ms'));
      }, delay);

      me.always(function () {
        clearTimeout(timer);
      });
    }

    return me;
  };

  // attach handler passing the resolve and reject functions
  handler(function (result) {
    _resolve(result);
  }, function (error) {
    _reject(error);
  });
}
450812
function utilCallbackAfterPromise(self, func, args, cb) {
	args = Array.prototype.slice.apply(args);
	let lastarg = args.pop();
	if(!cb) cb = lastarg;
	func.apply(self, args).then(res => {
		try {
			cb(null, res);
		} catch(e) {
			process.nextTick(() => { throw e });
		}
	}).catch((err) => {
		try {
			cb(err);
		} catch(e) {
			process.nextTick(() => { throw e });
		}
	});
}
555901
function createPromisable(wrapped) {
	return function (search, options, callback) {
		var self = this;
		var promisified = Promise.promisify(wrapped, { context: self });
		if (_.isFunction(options)) {
			callback = options;
		}
		if (!_.isFunction(callback)) {
			return {
				promise: function () {
					return promisified(search, options);
				},
				send: function (cb) {
					if (!_.isObject(options)) {
						options = cb;
					}
					wrapped.apply(self, [search, options, cb]);
				}
			};
		} else {
			wrapped.apply(self, [search, options, callback]);
		}
	}
}
767832
function awaitFunctionCall() {
                let func;
                const promise = new Promise((resolve, reject) => {
                    func = function(...args) {
                        resolve(args);
                        return new Promise((resolve, reject) => {
                            // give us some time to process the result before
                            // continuing
                            global.setTimeout(resolve, 1);
                        });
                    };
                });
                return {func, promise};
            }
214482
function(_first, _fun) {
      if(!_first) return _fun;
      return function(_value) {
        return _fun.call(this, _first.call(this, _value));
      };
    }
313946
function extend_if_promise(methods, ret) {
		if(ret && ret.then) {                                 // Check if return value is promise compatible
			return extend.promise(methods, ret);              // ..and if so, extend it, too.
		}
		return ret;                                           // ..and if not, return the same value.
	}
1103838
function _then(callback, resolve, reject) {
  return function (result) {
    try {
      var res = callback(result);
      if (res && typeof res.then === 'function' && typeof res['catch'] === 'function') {
        // method returned a promise
        res.then(resolve, reject);
      }
      else {
        resolve(res);
      }
    }
    catch (error) {
      reject(error);
    }
  }
}
678598
function promisify(data, prevent = false) {
  let promisified;

  if (typeof data === 'function') {
    promisified = (...args) => new Promise((resolve, reject) => {
      data.call(data, ...args.concat((error, ...args) => {
        error ? reject(error) : resolve.call(data, ...args);
      }));
    });
  } else if (typeof data === 'object' && false === prevent) {
    promisified = new Proxy({}, {
      get: (target, property) => promisify(data[property], true),
    });
  } else if (typeof data === 'string' && false === prevent) {
    promisified = promisify(require(data));
  } else {
    promisified = data;
  }

  return promisified;
}
80254
function (iter, fn) {
      return new Promise(function (resolve) {
        var i = 0;
        function next() {
          return iter.next().then(function (result) {
            if (result.done) {
              return result.value;
            }
            fn(result.value, i++);
            return next();
          });
        }
        resolve(next());
      });
    }
759531
function(value, ifFn, elseFn) {
			if ((ifFn && typeof ifFn !== 'function') || (elseFn && typeof elseFn !== 'function')) return apiRejection('ifFn and elseFn must be functions');

			var fn = value ? ifFn : elseFn;
			if (!fn) return Promise.resolve(value);

			return Promise.resolve(fn.call(this, value));
		}
1236460
function (args, result, func, extra) {
		return new SR.promise(function (resolve, reject) {
			UTIL.safeCall(func, args, result, function () {
				UTIL.safeCall(resolve);
			}, extra);
		});
	}
444234
function()
    {
        if (this._join.length) {

            var results = this._results;

            // Functions we need to call, that return a promise, for the value
            // we're recieving after subsequent joins
            var joins = [];

            this._join.forEach(function(join) {

                var f = function(outer) {
                    var inner = join.inner.toArray();

                    return inner.then(function(inner) {
                        var next = [];
                        outer.forEach(function(o) {
                            var oKey = o[join.outerKey];

                            // find the corresponding thing
                            var i = _(inner).find(function(i) {
                                return i[join.innerKey] == oKey;
                            });

                            if (i)
                                next.push(join.selector(o, i));

                        });
                        return next;
                    });
                };

                joins.push(f);
            });

            // Execute all of the joins, in order, feeding the result into the
            // next join. When they all are done, complete the promise for this
            // result
            return joins.reduce(Q.when, Q(results)).then(function(last) {
                return last;
            });

        } else {
            return Q(this._results);
        }
    }
337785
function(executor) {
            /*  optionally support non-constructor/plain-function call  */
            if (!(this instanceof Promise)) return new Promise(executor);
            /*  initialize object  */
            this.id = "Thenable/1.0.7";
            this.state = STATE_PENDING;
            /*  initial state  */
            this.fulfillValue = undefined;
            /*  initial value  */
            /*  [Promises/A+ 1.3, 2.1.2.2]  */
            this.rejectReason = undefined;
            /*  initial reason */
            /*  [Promises/A+ 1.5, 2.1.3.2]  */
            this.onFulfilled = [];
            /*  initial handlers  */
            this.onRejected = [];
            /*  initial handlers  */
            /*  support optional executor function  */
            if (typeof executor === "function") executor.call(this, this.fulfill.bind(this), this.reject.bind(this));
        }