diff --git a/bluebird/bluebird-tests.ts b/bluebird/bluebird-tests.ts index ade461074..bd4f46fc4 100644 --- a/bluebird/bluebird-tests.ts +++ b/bluebird/bluebird-tests.ts @@ -80,6 +80,7 @@ var voidProm: Promise; var fooProm: Promise; var barProm: Promise; +var fooOrBarProm: Promise; var bazProm: Promise; // - - - - - - - - - - - - - - - - - @@ -150,6 +151,7 @@ var BlueBird: typeof Promise; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - var nodeCallbackFunc = (callback: (err: any, result: string) => void) => {} +var nodeCallbackFuncErrorOnly = (callback: (err: any) => void) => {} // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -225,6 +227,21 @@ barProm = fooProm.then((value: Foo) => { }, (reason: any) => { return bar; }); +barProm = fooProm.then((value: Foo) => { + return bar; +}, (reason: any) => { + return barProm; +}); +barProm = fooProm.then((value: Foo) => { + return bar; +}, (reason: any) => { + return; +}); +barProm = fooProm.then((value: Foo) => { + return bar; +}, (reason: any) => { + return voidProm; +}); barProm = fooProm.then((value: Foo) => { return bar; }); @@ -236,36 +253,96 @@ barProm = barProm.then((value: Bar) => { // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -barProm = fooProm.catch((reason: any) => { +fooProm = fooProm.catch((reason: any) => { + return; +}); + +fooProm = fooProm.caught((reason: any) => { + return; +}); +fooProm = fooProm.catch((error: any) => { + return true; +}, (reason: any) => { + return; +}); +fooProm = fooProm.caught((error: any) => { + return true; +}, (reason: any) => { + return; +}); + +fooProm = fooProm.catch((reason: any) => { + return voidProm; +}); + +fooProm = fooProm.caught((reason: any) => { + return voidProm; +}); +fooProm = fooProm.catch((error: any) => { + return true; +}, (reason: any) => { + return voidProm; +}); +fooProm = fooProm.caught((error: any) => { + return true; +}, (reason: any) => { + return voidProm; +}); + +fooProm = fooProm.catch((reason: any) => { + //handle multiple valid return types simultaneously + if (true) { + return; + } else if (false) { + return voidProm; + } else if (foo) { + return foo; + } +}); + +fooOrBarProm = fooProm.catch((reason: any) => { return bar; }); -barProm = fooProm.caught((reason: any) => { +fooOrBarProm = fooProm.caught((reason: any) => { return bar; }); -barProm = fooProm.catch((reason: any) => { - return bar; +fooOrBarProm = fooProm.catch((error: any) => { + return true; }, (reason: any) => { return bar; }); -barProm = fooProm.caught((reason: any) => { - return bar; +fooOrBarProm = fooProm.caught((error: any) => { + return true; }, (reason: any) => { return bar; }); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -barProm = fooProm.catch(Error, (reason: any) => { +fooProm = fooProm.catch(Error, (reason: any) => { + return; +}); +fooProm = fooProm.catch(Promise.CancellationError, (reason: any) => { + return; +}); +fooProm = fooProm.caught(Error, (reason: any) => { + return; +}); +fooProm = fooProm.caught(Promise.CancellationError, (reason: any) => { + return; +}); + +fooOrBarProm = fooProm.catch(Error, (reason: any) => { return bar; }); -barProm = fooProm.catch(Promise.CancellationError, (reason: any) => { +fooOrBarProm = fooProm.catch(Promise.CancellationError, (reason: any) => { return bar; }); -barProm = fooProm.caught(Error, (reason: any) => { +fooOrBarProm = fooProm.caught(Error, (reason: any) => { return bar; }); -barProm = fooProm.caught(Promise.CancellationError, (reason: any) => { +fooOrBarProm = fooProm.caught(Promise.CancellationError, (reason: any) => { return bar; }); @@ -678,6 +755,7 @@ func = Promise.promisify(f, obj); obj = Promise.promisifyAll(obj); anyProm = Promise.fromNode(callback => nodeCallbackFunc(callback)); +anyProm = Promise.fromNode(callback => nodeCallbackFuncErrorOnly(callback)); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/bluebird/bluebird.d.ts b/bluebird/bluebird.d.ts index 878e1051e..9f36cf5bc 100644 --- a/bluebird/bluebird.d.ts +++ b/bluebird/bluebird.d.ts @@ -25,19 +25,19 @@ declare class Promise implements Promise.Thenable, Promise.Inspection { /** * Promises/A+ `.then()` with progress handler. Returns a new promise chained from this promise. The new promise will be rejected or resolved dedefer on the passed `fulfilledHandler`, `rejectedHandler` and the state of this promise. */ - then(onFulfill: (value: R) => U|Promise.Thenable, onReject: (error: any) => Promise.Thenable, onProgress?: (note: any) => any): Promise; - then(onFulfill: (value: R) => U|Promise.Thenable, onReject?: (error: any) => U, onProgress?: (note: any) => any): Promise; - + then(onFulfill: (value: R) => U|Promise.Thenable, onReject?: (error: any) => U|Promise.Thenable, onProgress?: (note: any) => any): Promise; + then(onFulfill: (value: R) => U|Promise.Thenable, onReject?: (error: any) => void|Promise.Thenable, onProgress?: (note: any) => any): Promise; + /** * This is a catch-all exception handler, shortcut for calling `.then(null, handler)` on this promise. Any exception happening in a `.then`-chain will propagate to nearest `.catch` handler. * * Alias `.caught();` for compatibility with earlier ECMAScript version. */ - catch(onReject?: (error: any) => Promise.Thenable): Promise; - caught(onReject?: (error: any) => Promise.Thenable): Promise; + catch(onReject?: (error: any) => R|Promise.Thenable|void|Promise.Thenable): Promise; + caught(onReject?: (error: any) => R|Promise.Thenable|void|Promise.Thenable): Promise; - catch(onReject?: (error: any) => U): Promise; - caught(onReject?: (error: any) => U): Promise; + catch(onReject?: (error: any) => U|Promise.Thenable): Promise; + caught(onReject?: (error: any) => U|Promise.Thenable): Promise; /** * This extends `.catch` to work more like catch-clauses in languages like Java or C#. Instead of manually checking `instanceof` or `.name === "SomeError"`, you may specify a number of error constructors which are eligible for this catch handler. The catch handler that is first met that has eligible constructors specified, is the one that will be called. @@ -46,17 +46,18 @@ declare class Promise implements Promise.Thenable, Promise.Inspection { * * Alias `.caught();` for compatibility with earlier ECMAScript version. */ - catch(predicate: (error: any) => boolean, onReject: (error: any) => Promise.Thenable): Promise; - caught(predicate: (error: any) => boolean, onReject: (error: any) => Promise.Thenable): Promise; + catch(predicate: (error: any) => boolean, onReject: (error: any) => R|Promise.Thenable|void|Promise.Thenable): Promise; + caught(predicate: (error: any) => boolean, onReject: (error: any) => R|Promise.Thenable|void|Promise.Thenable): Promise; - catch(predicate: (error: any) => boolean, onReject: (error: any) => U): Promise; - caught(predicate: (error: any) => boolean, onReject: (error: any) => U): Promise; + catch(predicate: (error: any) => boolean, onReject: (error: any) => U|Promise.Thenable): Promise; + caught(predicate: (error: any) => boolean, onReject: (error: any) => U|Promise.Thenable): Promise; - catch(ErrorClass: Function, onReject: (error: any) => Promise.Thenable): Promise; - caught(ErrorClass: Function, onReject: (error: any) => Promise.Thenable): Promise; + catch(ErrorClass: Function, onReject: (error: any) => R|Promise.Thenable|void|Promise.Thenable): Promise; + caught(ErrorClass: Function, onReject: (error: any) => R|Promise.Thenable|void|Promise.Thenable): Promise; + + catch(ErrorClass: Function, onReject: (error: any) => U|Promise.Thenable): Promise; + caught(ErrorClass: Function, onReject: (error: any) => U|Promise.Thenable): Promise; - catch(ErrorClass: Function, onReject: (error: any) => U): Promise; - caught(ErrorClass: Function, onReject: (error: any) => U): Promise; /** * Like `.catch` but instead of catching all types of exceptions, it only catches those that don't originate from thrown errors but rather from explicit rejections. @@ -426,7 +427,7 @@ declare class Promise implements Promise.Thenable, Promise.Inspection { /** * Returns a promise that is resolved by a node style callback function. */ - static fromNode(resolver: (callback: (err: any, result: any) => void) => void): Promise; + static fromNode(resolver: (callback: (err: any, result?: any) => void) => void): Promise; /** * Returns a function that can use `yield` to run asynchronous code synchronously. This feature requires the support of generators which are drafted in the next version of the language. Node version greater than `0.11.2` is required and needs to be executed with the `--harmony-generators` (or `--harmony`) command-line switch. @@ -671,8 +672,8 @@ declare module Promise { export function OperationalError(): OperationalError; export interface Thenable { - then(onFulfilled: (value: R) => U|Thenable, onRejected: (error: any) => Thenable): Thenable; - then(onFulfilled: (value: R) => U|Thenable, onRejected?: (error: any) => U): Thenable; + then(onFulfilled: (value: R) => U|Thenable, onRejected?: (error: any) => U|Thenable): Thenable; + then(onFulfilled: (value: R) => U|Thenable, onRejected?: (error: any) => void|Thenable): Thenable; } export interface Resolver {