// Type definitions for When 2.4.0 // Project: https://github.com/cujojs/when // Definitions by: Derek Cicerone , Wim Looman // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare function When(): When.Promise; declare function When(promiseOrValue: T | When.Promise | When.Thenable): When.Promise; declare function When(promiseOrValue: T | When.Promise | When.Thenable, transform: (val: T) => U): When.Promise; declare namespace When { // Helper interfaces module _ { interface Fn0 { (): T } interface Fn1 { (a1: A1): T } interface Fn2 { (a1: A1, a2: A2): T } interface Fn3 { (a1: A1, a2: A2, a3: A3): T } interface Fn4 { (a1: A1, a2: A2, a3: A3, a4: A4): T } interface Fn5 { (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5): T } interface Fn6 { (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6): T } interface LiftedFn0 extends Fn0> { } interface LiftedFn1 extends Fn1, Promise> { } interface LiftedFn2 extends Fn2, A2 | Promise, Promise> { } interface LiftedFn3 extends Fn3, A2 | Promise, A3 | Promise, Promise> { } interface LiftedFn4 extends Fn4, A2 | Promise, A3 | Promise, A4 | Promise, Promise> { } interface LiftedFn5 extends Fn5, A2 | Promise, A3 | Promise, A4 | Promise, A5 | Promise, Promise> { } interface NodeCallback { (err: any, result: T): void } interface NodeFn0 extends _.Fn1, void> { } interface NodeFn1 extends _.Fn2, void> { } interface NodeFn2 extends _.Fn3, void> { } interface NodeFn3 extends _.Fn4, void> { } interface NodeFn4 extends _.Fn5, void> { } interface NodeFn5 extends _.Fn6, void> { } } function attempt( f: _.Fn0 ): Promise; function attempt( f: _.Fn1, arg1: A1 | Promise ): Promise; function attempt( f: _.Fn2, arg1: A1 | Promise, arg2: A2 | Promise ): Promise; function attempt( f: _.Fn3, arg1: A1 | Promise, arg2: A2 | Promise, arg3: A3 | Promise ): Promise; function attempt( f: _.Fn4, arg1: A1 | Promise, arg2: A2 | Promise, arg3: A3 | Promise, arg4: A4 | Promise ): Promise; function attempt( f: _.Fn5, arg1: A1 | Promise, arg2: A2 | Promise, arg3: A3 | Promise, arg4: A4 | Promise, arg5: A5 | Promise ): Promise; function lift(f: _.Fn0): _.LiftedFn0; function lift(f: _.Fn1): _.LiftedFn1; function lift(f: _.Fn2): _.LiftedFn2; function lift(f: _.Fn3): _.LiftedFn3; function lift(f: _.Fn4): _.LiftedFn4; function lift(f: _.Fn5): _.LiftedFn5; function promise(resolver: (resolve: (value: T) => void, reject: (reason: any) => void) => void): Promise; function reject(reason: any): Promise; /** * Return a promise that will resolve only once all the supplied promisesOrValues * have resolved. The resolution value of the returned promise will be an array * containing the resolution values of each of the promisesOrValues. * @memberOf when * * @param promisesOrValues array of anything, may contain a mix * of {@link Promise}s and values */ function all(promisesOrValues: any[]): Promise; /** * Promise-aware array map function, similar to `Array.prototype.map()`, * but input array may contain promises or values. * @param promisesOrValues array of anything, may contain a mix of {@link Promise}s and values * @param mapFunc map function which may return a promise or value * @returns a promise that will fulfill with an array of mapped values * or reject if any input promise rejects. */ function map(promisesOrValues: any[], mapFunc: (value: any, index: number) => any): Promise; /** * Traditional reduce function, similar to `Array.prototype.reduce()`, but * input may contain promises and/or values, and reduceFunc * may return either a value or a promise, *and* initialValue may * be a promise for the starting value. * @param promisesOrValues array or promise for an array of anything, * may contain a mix of promises and values. * @param reduceFunc function(accumulated:*, x:*, index:number):*} f reduce function * @returns a promise that will resolve to the final reduced value */ function reduce(promisesOrValues: any[], reduceFunc: (reduction: T, value: any, index: number) => T | Promise, initialValue: T): Promise; /** * Traditional reduce function, similar to `Array.prototype.reduceRight()`, but * input may contain promises and/or values, and reduceFunc * may return either a value or a promise, *and* initialValue may * be a promise for the starting value. * @param promisesOrValues array or promise for an array of anything, * may contain a mix of promises and values. * @param reduceFunc function(accumulated:*, x:*, index:number):*} f reduce function * @returns a promise that will resolve to the final reduced value */ function reduceRight(promisesOrValues: any[], reduceFunc: (reduction: T, value: any, index: number) => T | Promise, initialValue: T): Promise; /** * Describes the outcome of a promise. * state may be one of: * "fulfilled" - the promise has resolved * "rejected" - the promise has rejected */ type Descriptor = FulfilledDescriptor | RejectedDescriptor; /** * Snapshot which describes the status of a promise. * state may be one of: * "fulfilled" - the promise has resolved * "rejected" - the promise has rejected * "pending" - the promise is still pending to resolve/reject */ type Snapshot = FulfilledDescriptor | RejectedDescriptor | PendingDescriptor; interface FulfilledDescriptor { state: 'fulfilled'; value: T; } interface RejectedDescriptor { state: 'rejected'; reason: any; } interface PendingDescriptor { state: 'pending'; } /** * Returns a promise for an array containing the same number of elements as the input array. * Each element is a descriptor object describing of the outcome of the corresponding element in the input. * The returned promise will only reject if array itself is a rejected promise. Otherwise, * it will always fulfill with an array of descriptors. This is in contrast to when.all, * which will reject if any element of array rejects. * @memberOf when * * @param promisesOrValues array of anything, may contain a mix * of {@link Promise}s and values */ function settle(promisesOrValues: any[]): Promise[]>; /** * Generates a potentially infinite stream of promises by repeatedly calling f until predicate becomes true. * @memberOf when * @param f function that, given a seed, returns the next value or a promise for it. * @param predicate function that receives the current iteration value, and should return truthy when the iterating should stop * @param handler function that receives each value as it is produced by f. It may return a promise to delay the next iteration. * @param seed initial value provided to the handler, and first f invocation. May be a promise. */ function iterate(f: (seed: U) => U | Promise, predicate: (value: U) => boolean, handler: (value: U) => Promise | void, seed: U | Promise): Promise; /** * Similar to when/iterate, when.unfold generates a potentially infinite stream of promises by repeatedly calling * unspool until predicate becomes true. when.unfold allows you to thread additional state information through the iteration. * @memberOf when * @param unspool function that, given a seed, returns a [valueToSendToHandler, newSeed] pair. * May return an array, array of promises, promise for an array, or promise for an array of promises. * @param predicate function that receives the current seed, and should return truthy when the unfold should stop * @param handler function that receives the valueToSendToHandler of the current iteration. * This function can process valueToSendToHandler in whatever way you need. * It may return a promise to delay the next iteration of the unfold. * @param seed initial value provided to the first unspool invocation. May be a promise. */ function unfold(unspool: (seed: U) => [T | Promise, U | Promise] | Promise<[T | Promise, U | Promise]>, predicate: (value: U) => boolean | Promise, handler: (value: T) => Promise | void, seed: U | Promise): Promise; /** * Creates a {promise, resolver} pair, either or both of which * may be given out safely to consumers. * The resolver has resolve, reject, and progress. The promise * has then plus extended promise API. */ function defer(): Deferred; /** * Joins multiple promises into a single returned promise. * @return a promise that will fulfill when *all* the input promises * have fulfilled, or will reject when *any one* of the input promises rejects. */ function join(...promises: Promise[]): Promise; /** * Joins multiple promises into a single returned promise. * @return a promise that will fulfill when *all* the input promises * have fulfilled, or will reject when *any one* of the input promises rejects. */ function join(...promises: any[]): Promise; /** * Returns a resolved promise. The returned promise will be * - fulfilled with promiseOrValue if it is a value, or * - if promiseOrValue is a promise * - fulfilled with promiseOrValue's value after it is fulfilled * - rejected with promiseOrValue's reason after it is rejected */ function resolve(): Promise; function resolve(promiseOrValue: T | Promise | Thenable): Promise; interface Deferred { notify(update: any): void; promise: Promise; reject(reason: any): void; resolve(value?: T): void; resolve(value?: Promise): void; } interface Promise { catch(onRejected?: (reason: any) => U | Promise): Promise; catch(filter: (reason: any) => boolean, onRejected?: (reason: any) => U | Promise): Promise; // Make sure you test any usage of these overloads, exceptionType must // be a constructor with prototype set to an instance of Error. catch(exceptionType: any, onRejected?: (reason: any) => U | Promise): Promise; finally(onFulfilledOrRejected: Function): Promise; ensure(onFulfilledOrRejected: Function): Promise; inspect(): Snapshot; yield(value: U | Promise): Promise; else(value: T): Promise; orElse(value: T): Promise; tap(onFulfilledSideEffect: (value: T) => void): Promise; delay(milliseconds: number): Promise; timeout(milliseconds: number, reason?: any): Promise; with(thisArg: any): Promise; withThis(thisArg: any): Promise; otherwise(onRejected?: (reason: any) => U | Promise): Promise; otherwise(predicate: (reason: any) => boolean, onRejected?: (reason: any) => U | Promise): Promise; // Make sure you test any usage of these overloads, exceptionType must // be a constructor with prototype set to an instance of Error. otherwise(exceptionType: any, onRejected?: (reason: any) => U | Promise): Promise; then( onFulfilled: ((value: T) => TResult1 | Thenable), onRejected: ((reason: any) => TResult2 | Thenable), onProgress?: (update: any) => void ): Promise; then( onFulfilled: ((value: T) => TResult | Thenable), onRejected?: ((reason: any) => TResult | Thenable) | undefined | null, onProgress?: (update: any) => void ): Promise; then( onFulfilled: ((value: T) => T | Thenable) | undefined | null, onRejected: ((reason: any) => TResult | Thenable), onProgress?: (update: any) => void ): Promise; then( onFulfilled?: ((value: T) => T | Thenable) | undefined | null, onRejected?: ((reason: any) => T | Thenable) | undefined | null, onProgress?: (update: any) => void ): Promise; spread(onFulfilled: _.Fn0 | T>): Promise; spread(onFulfilled: _.Fn1 | T>): Promise; spread(onFulfilled: _.Fn2 | T>): Promise; spread(onFulfilled: _.Fn3 | T>): Promise; spread(onFulfilled: _.Fn4 | T>): Promise; spread(onFulfilled: _.Fn5 | T>): Promise; done(onFulfilled?: (value: T) => void, onRejected?: (reason: any) => void): void; fold(combine: (value1: T, value2: V) => U | Promise, value2: V | Promise): Promise; } interface Thenable { then(onFulfilled?: (value: T) => U, onRejected?: (reason: any) => U): Thenable; } } declare module "when" { export = When; } declare module "when/node" { import when = require('when'); import _ = when._; function lift(fn: _.NodeFn0): _.LiftedFn0; function lift(fn: _.NodeFn1): _.LiftedFn1; function lift(fn: _.NodeFn2): _.LiftedFn2; function lift(fn: _.NodeFn3): _.LiftedFn3; function lift(fn: _.NodeFn4): _.LiftedFn4; function lift(fn: _.NodeFn5): _.LiftedFn5; function call( fn: _.NodeFn0 ): when.Promise; function call( fn: _.NodeFn1, arg1: A1 | when.Promise ): when.Promise; function call( fn: _.NodeFn2, arg1: A1 | when.Promise, arg2: A2 | when.Promise ): when.Promise; function call( fn: _.NodeFn3, arg1: A1 | when.Promise, arg2: A2 | when.Promise, arg3: A3 | when.Promise ): when.Promise; function call( fn: _.NodeFn4, arg1: A1 | when.Promise, arg2: A2 | when.Promise, arg3: A3 | when.Promise, arg4: A4 | when.Promise ): when.Promise; function call( fn: _.NodeFn5, arg1: A1 | when.Promise, arg2: A2 | when.Promise, arg3: A3 | when.Promise, arg4: A4 | when.Promise, arg5: A5 | when.Promise ): when.Promise; function apply(fn: _.NodeFn0, args: any[] | IArguments): when.Promise; function apply(fn: _.NodeFn1, args: any[] | IArguments): when.Promise; function apply(fn: _.NodeFn2, args: any[] | IArguments): when.Promise; function apply(fn: _.NodeFn3, args: any[] | IArguments): when.Promise; function apply(fn: _.NodeFn4, args: any[] | IArguments): when.Promise; function apply(fn: _.NodeFn5, args: any[] | IArguments): when.Promise; function liftAll(srcApi: any, transform?: (destApi: any, liftedFunc: Function, name: string) => any, destApi?: any): any; function liftCallback(callback: (err: any, arg: TArg) => void): (value: when.Promise) => when.Promise; function bindCallback(arg: when.Promise, callback: (err: any, arg: TArg) => void): when.Promise; interface Resolver { reject(reason: any): void; resolve(value?: T | when.Promise): void; } function createCallback(resolver: Resolver): (err: any, arg: TArg) => void; }