// Type definitions for When 2.4.0 // Project: https://github.com/cujojs/when // Definitions by: Derek Cicerone , Wim Looman // Definitions: https://github.com/borisyankov/DefinitelyTyped declare function When(value: When.Promise): When.Promise; declare function When(value: When.Thenable): When.Promise; declare function When(value: T): When.Promise; declare function When(value: When.Promise, transform: (val: T) => U): When.Promise; declare function When(value: When.Thenable, transform: (val: T) => U): When.Promise; declare function When(value: T, transform: (val: T) => U): When.Promise; declare module 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; /** * Describes the status of a promise. * state may be one of: * "fulfilled" - the promise has resolved * "pending" - the promise is still pending to resolve/reject * "rejected" - the promise has rejected */ interface Descriptor { state: string; value?: T; reason?: any; } /** * 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[]>; /** * 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: Promise): Promise; function resolve(foreign: Thenable): Promise; function resolve(value?: T): 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) => U | Promise, onRejected?: (reason: any) => U | Promise, 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; } interface Snapshot { state: string; value?: T; reason?: any; } } 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[]): when.Promise; function apply(fn: _.NodeFn1, args: any[]): when.Promise; function apply(fn: _.NodeFn2, args: any[]): when.Promise; function apply(fn: _.NodeFn3, args: any[]): when.Promise; function apply(fn: _.NodeFn4, args: any[]): when.Promise; function apply(fn: _.NodeFn5, args: any[]): 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): void; resolve(value?: when.Promise): void; } function createCallback(resolver: Resolver): (err: any, arg: TArg) => void; }