Converts thenables to Q promises.
@param promise thenable promise
@returns a Q promise
184297
function coerce(promise) {
        var deferred = defer();
        nextTick(function () {
            try {
                promise.then(deferred.resolve, deferred.reject, deferred.notify);
            } catch (exception) {
                deferred.reject(exception);
            }
        });
        return deferred.promise;
    }
467394
function promiseSeries(items, iterator) {
	return items.reduce((iterable, name) => {
		return iterable.then(() => iterator(name));
	}, Promise.resolve());
}
323901
function handlePromise(query, q, model, resultsPerPage) {
  return Promise.all([
    query.exec(),
    model.count(q).exec()
  ]).spread(function(results, count) {
    return [
      results,
      Math.ceil(count / resultsPerPage) || 1,
      count
    ]
  });
}
19762
function cancelableify(promise) {
      promise.cancel = cancel;
      var then = promise.then;
      promise.then = function() {
        var newPromise = then.apply(this, arguments);
        return cancelableify(newPromise);
      };
      return promise;
    }
1238465
@SafeVarargs
    public static <T> PromisesArray<T> ofPromises(Promise<T>... items) {
        ArrayList<Promise<T>> res = new ArrayList<>();
        Collections.addAll(res, items);
        final Promise[] promises = res.toArray(new Promise[res.size()]);
        return new PromisesArray<>((PromiseFunc<Promise<T>[]>) executor -> {
            executor.result(promises);
        });
    }
118226
@ObjectiveCName("flatMap:")
    public <R> Promise<R> flatMap(final Function<T, Promise<R>> res) {
        final Promise<T> self = this;
        return new Promise<>((PromiseFunc<R>) resolver -> {
            self.then(t -> {
                Promise<R> promise;
                try {
                    promise = res.apply(t);
                } catch (Exception e) {
                    e.printStackTrace();
                    resolver.tryError(e);
                    return;
                }

                promise.then(t2 -> resolver.result(t2));
                promise.failure(e -> resolver.error(e));
            });
            self.failure(e -> resolver.error(e));
        });
    }
251030
public static final Promise resolve() {
		return new Promise(CompletableFuture.completedFuture(new Tree((Tree) null, null, null)));
	}
572945
function Promise(executor) {
  if (typeof executor !== 'function') {
    throw new TypeError();
  }

  if (!(this instanceof Promise)) {
    return new Promise(executor);
  }

  if (executor !== blessed.be) {
    blessed.be(this, executor, true);
  }
}
295703
function Q(value) {
    // If the object is already a Promise, return it directly.  This enables
    // the resolve function to both be used to created references from objects,
    // but to tolerably coerce non-promises to promises.
    if (value instanceof Promise) {
        return value;
    }

    // assimilate thenables
    if (isPromiseAlike(value)) {
        return coerce(value);
    } else {
        return fulfill(value);
    }
}
660990
@Contract(pure = true)
	@NotNull
	public static <T> Promise<T> schedule(@NotNull Promise<T> promise, long timestamp) {
		MaterializedPromise<T> materializedPromise = promise.materialize();
		return Promise.ofCallback(cb ->
				getCurrentEventloop().schedule(timestamp, () -> materializedPromise.whenComplete(cb)));
	}
564522
function reversePromises (promises) {
    return promises.map(function (promise) {
      var d = $q.defer();
      promise.then(d.reject, d.resolve);
      return d.promise;
    });
  }
1204086
@ObjectiveCName("tupleWithT1:withT2:")
    public static <T1, T2> Promise<Tuple2<T1, T2>> tuple(Promise<T1> t1, Promise<T2> t2) {

        return PromisesArray.ofPromises((Promise<Object>) t1, (Promise<Object>) t2)
                .zip()
                .map(src -> new Tuple2<>((T1) src.get(0), (T2) src.get(1)));
    }
1235057
@Contract(pure = true)
	@NotNull
	public static <T> Promise<T[]> toArray(@NotNull Class<T> type, @NotNull List<? extends Promise<? extends T>> promises) {
		int size = promises.size();
		if (size == 0) return toArray(type);
		if (size == 1) return toArray(type, promises.get(0));
		if (size == 2) return toArray(type, promises.get(0), promises.get(1));

		@SuppressWarnings("unchecked") PromiseToArray<T> resultPromise = new PromiseToArray<>((T[]) Array.newInstance(type, size));

		for (int i = 0; i < size; i++) {
			Promise<? extends T> promise = promises.get(i);
			if (promise.isResult()) {
				resultPromise.accumulator[i] = promise.materialize().getResult();
				continue;
			}
			if (promise.isException()) return Promise.ofException(promise.materialize().getException());
			int index = i;
			resultPromise.countdown++;
			promise.whenComplete((result, e) -> {
				if (e == null) {
					resultPromise.processComplete(result, index);
				} else {
					resultPromise.tryCompleteExceptionally(e);
				}
			});
		}
		return resultPromise.countdown != 0 ? resultPromise : Promise.of(resultPromise.accumulator);
	}
1059281
@SafeVarargs
  public static <V> PnkyPromise<List<V>> allFailingFast(final PnkyPromise<? extends V>... promises)
  {
    return allFailingFast(Lists.newArrayList(promises));
  }
900428
@SuppressWarnings("unchecked")
    public static <T> PromisesArray<T> of(Collection<T> collection) {
        final ArrayList<Promise<T>> res = new ArrayList<>();
        for (T t : collection) {
            res.add(Promise.success(t));
        }
        final Promise[] promises = res.toArray(new Promise[res.size()]);
        return new PromisesArray<>((PromiseFunc<Promise<T>[]>) executor -> {
            executor.result(promises);
        });
    }
403293
function (azurePromise) {
      var deferred = $q.defer();
      azurePromise.done(function (items) {
        deferred.resolve(items);
      }, function (err) {
        deferred.reject(err);
      });
      return deferred.promise;
    }
212132
function fin(promise, finalPromiseFactory) {
  return promise.then(function (res) {
    return finalPromiseFactory().then(function () {
      return res;
    });
  }, function (reason) {
    return finalPromiseFactory().then(function () {
      throw reason;
    });
  });
}