diff --git a/when/when.d.ts b/when/when.d.ts index 5db15b316..be5e25f15 100644 --- a/when/when.d.ts +++ b/when/when.d.ts @@ -12,29 +12,79 @@ declare function When(value: When.Thenable, transform: (val: T) => U): declare function When(value: T, transform: (val: T) => U): When.Promise; declare module When { - function attempt(f: () => T): Promise; + // 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 } - function attempt(f: (a: A) => T, a: Promise): Promise; - function attempt(f: (a: A) => T, a: A): Promise; + 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> { } - function attempt(f: (a: A, b: B) => T, a: Promise, b: Promise): Promise; - function attempt(f: (a: A, b: B) => T, a: Promise, b: B): Promise; - function attempt(f: (a: A, b: B) => T, a: A, b: Promise): Promise; - function attempt(f: (a: A, b: B) => T, a: A, b: B): Promise; + interface NodeCallback { (err: any, result: T): void } - function attempt(f: (a: A, b: B, c: C) => T, a: Promise, b: Promise, c: Promise): Promise; - function attempt(f: (a: A, b: B, c: C) => T, a: Promise, b: Promise, c: C): Promise; - function attempt(f: (a: A, b: B, c: C) => T, a: Promise, b: B, c: Promise): Promise; - function attempt(f: (a: A, b: B, c: C) => T, a: Promise, b: B, c: C): Promise; - function attempt(f: (a: A, b: B, c: C) => T, a: A, b: Promise, c: Promise): Promise; - function attempt(f: (a: A, b: B, c: C) => T, a: A, b: Promise, c: C): Promise; - function attempt(f: (a: A, b: B, c: C) => T, a: A, b: B, c: Promise): Promise; - function attempt(f: (a: A, b: B, c: C) => T, a: A, b: B, c: C): Promise; + 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 lift(f: () => T): () => Promise; - function lift(f: (a: A) => T): (a: Promise) => Promise; - function lift(f: (a: A, b: B) => T): (a: Promise, b: Promise) => Promise; - function lift(f: (a: A, b: B, c: C) => T): (a: Promise, b: Promise, c: Promise) => Promise; + 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; @@ -92,16 +142,13 @@ declare module When { } interface Promise { - catch(onRejected?: (reason: any) => Promise): Promise; - catch(onRejected?: (reason: any) => U): Promise; + catch(onRejected?: (reason: any) => U | Promise): Promise; - catch(filter: (reason: any) => boolean, onRejected?: (reason: any) => Promise): Promise; - catch(filter: (reason: any) => boolean, onRejected?: (reason: any) => U): 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) => Promise): Promise; - catch(exceptionType: any, onRejected?: (reason: any) => U): Promise; finally(onFulfilledOrRejected: Function): Promise; @@ -109,8 +156,7 @@ declare module When { inspect(): Snapshot; - yield(value: Promise): Promise; - yield(value: U): Promise; + yield(value: U | Promise): Promise; else(value: T): Promise; orElse(value: T): Promise; @@ -124,29 +170,19 @@ declare module When { with(thisArg: any): Promise; withThis(thisArg: any): Promise; - otherwise(onRejected?: (reason: any) => Promise): Promise; - otherwise(onRejected?: (reason: any) => U): Promise; + otherwise(onRejected?: (reason: any) => U | Promise): Promise; - otherwise(predicate: (reason: any) => boolean, onRejected?: (reason: any) => Promise): Promise; - otherwise(predicate: (reason: any) => boolean, onRejected?: (reason: any) => U): 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) => Promise): Promise; - otherwise(exceptionType: any, onRejected?: (reason: any) => U): Promise; + otherwise(exceptionType: any, onRejected?: (reason: any) => U | Promise): Promise; - then(onFulfilled: (value: T) => Promise, onRejected?: (reason: any) => Promise, onProgress?: (update: any) => void): Promise; - then(onFulfilled: (value: T) => Promise, onRejected?: (reason: any) => U, onProgress?: (update: any) => void): Promise; - then(onFulfilled: (value: T) => U, onRejected?: (reason: any) => Promise, onProgress?: (update: any) => void): Promise; - then(onFulfilled: (value: T) => U, onRejected?: (reason: any) => U, onProgress?: (update: any) => void): Promise; + then(onFulfilled: (value: T) => U | Promise, onRejected?: (reason: any) => U | Promise, onProgress?: (update: any) => void): Promise; done(onFulfilled: (value: T) => void, onRejected?: (reason: any) => void): void; - fold(combine: (value1: T, value2: V) => Promise, value2: V): Promise; - fold(combine: (value1: T, value2: V) => Promise, value2: Promise): Promise; - - fold(combine: (value1: T, value2: V) => U, value2: V): Promise; - fold(combine: (value1: T, value2: V) => U, value2: Promise): Promise; + fold(combine: (value1: T, value2: V) => U | Promise, value2: V | Promise): Promise; } interface Thenable { @@ -166,42 +202,67 @@ declare module "when" { declare module "when/node" { import when = require('when'); + import _ = when._; - function lift(fn: (callback: (err: any, result: TResult) => void) => void): () => when.Promise; - function lift(fn: (arg1: TArg1, callback: (err: any, result: TResult) => void) => void): (arg1: when.Promise) => when.Promise; - function lift(fn: (arg1: TArg1, arg2: TArg2, callback: (err: any, result: TResult) => void) => void): (arg1: when.Promise, arg2: when.Promise) => when.Promise; - function lift(fn: (arg1: TArg1, arg2: TArg2, arg3: TArg3, callback: (err: any, result: TResult) => void) => void): (arg1: when.Promise, arg2: when.Promise, arg3: when.Promise) => when.Promise; + 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 call(fn: (callback: (err: any, result: TResult) => void) => void): when.Promise; - - function call(fn: (arg1: TArg1, callback: (err: any, result: TResult) => void) => void, arg1: TArg1): when.Promise; - function call(fn: (arg1: TArg1, callback: (err: any, result: TResult) => void) => void, arg1: when.Promise): when.Promise; - - function call(fn: (arg1: TArg1, arg2: TArg2, callback: (err: any, result: TResult) => void) => void, arg1: TArg1, arg2: TArg2): when.Promise; - function call(fn: (arg1: TArg1, arg2: TArg2, callback: (err: any, result: TResult) => void) => void, arg1: when.Promise, arg2: TArg2): when.Promise; - function call(fn: (arg1: TArg1, arg2: TArg2, callback: (err: any, result: TResult) => void) => void, arg1: TArg1, arg2: when.Promise): when.Promise; - function call(fn: (arg1: TArg1, arg2: TArg2, callback: (err: any, result: TResult) => void) => void, arg1: when.Promise, arg2: when.Promise): when.Promise; - - function call(fn: (arg1: TArg1, arg2: TArg2, arg3: TArg3, callback: (err: any, result: TResult) => void) => void, arg1: TArg1, arg2: TArg2, arg3: TArg3): when.Promise; - function call(fn: (arg1: TArg1, arg2: TArg2, arg3: TArg3, callback: (err: any, result: TResult) => void) => void, arg1: TArg1, arg2: TArg2, arg3: when.Promise): when.Promise; - function call(fn: (arg1: TArg1, arg2: TArg2, arg3: TArg3, callback: (err: any, result: TResult) => void) => void, arg1: TArg1, arg2: when.Promise, arg3: TArg3): when.Promise; - function call(fn: (arg1: TArg1, arg2: TArg2, arg3: TArg3, callback: (err: any, result: TResult) => void) => void, arg1: TArg1, arg2: when.Promise, arg3: when.Promise): when.Promise; - function call(fn: (arg1: TArg1, arg2: TArg2, arg3: TArg3, callback: (err: any, result: TResult) => void) => void, arg1: when.Promise, arg2: TArg2, arg3: TArg3): when.Promise; - function call(fn: (arg1: TArg1, arg2: TArg2, arg3: TArg3, callback: (err: any, result: TResult) => void) => void, arg1: when.Promise, arg2: TArg2, arg3: when.Promise): when.Promise; - function call(fn: (arg1: TArg1, arg2: TArg2, arg3: TArg3, callback: (err: any, result: TResult) => void) => void, arg1: when.Promise, arg2: when.Promise, arg3: TArg3): when.Promise; - function call(fn: (arg1: TArg1, arg2: TArg2, arg3: TArg3, callback: (err: any, result: TResult) => void) => void, arg1: when.Promise, arg2: when.Promise, arg3: when.Promise): when.Promise; - - - function apply(fn: (callback: (err: any, result: TResult) => void) => void, args: any[]): when.Promise; - function apply(fn: (arg1: any, callback: (err: any, result: TResult) => void) => void, args: any[]): when.Promise; - function apply(fn: (arg1: any, arg2: any, callback: (err: any, result: TResult) => void) => void, args: any[]): when.Promise; - function apply(fn: (arg1: any, arg2: any, arg3: any, callback: (err: any, result: TResult) => void) => void, args: any[]): when.Promise; - - function liftCallback(callback: (err: any, arg: TArg) => void): (value: when.Promise) => when.Promise;