From 6824b8c8e619c340c360f25f3909ff9e8ac792fc Mon Sep 17 00:00:00 2001 From: Eric Nicholson Date: Tue, 10 Nov 2015 20:21:43 -0500 Subject: [PATCH 1/3] Allow calls to fromNode without a result parameter in the callback --- bluebird/bluebird-tests.ts | 2 ++ bluebird/bluebird.d.ts | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/bluebird/bluebird-tests.ts b/bluebird/bluebird-tests.ts index ade461074..440743c03 100644 --- a/bluebird/bluebird-tests.ts +++ b/bluebird/bluebird-tests.ts @@ -150,6 +150,7 @@ var BlueBird: typeof Promise; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - var nodeCallbackFunc = (callback: (err: any, result: string) => void) => {} +var nodeCallbackFuncErrorOnly = (callback: (err: any) => void) => {} // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -678,6 +679,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..67067dc5b 100644 --- a/bluebird/bluebird.d.ts +++ b/bluebird/bluebird.d.ts @@ -426,7 +426,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. From 0ed32474bdea7d09f556f289ffbfcdd7adb55485 Mon Sep 17 00:00:00 2001 From: Eric Nicholson Date: Tue, 10 Nov 2015 21:04:56 -0500 Subject: [PATCH 2/3] Added better catch definitions that pass through the original promise type or type union of promise|rejection type --- bluebird/bluebird-tests.ts | 81 +++++++++++++++++++++++++++++++++----- bluebird/bluebird.d.ts | 25 ++++++------ 2 files changed, 84 insertions(+), 22 deletions(-) diff --git a/bluebird/bluebird-tests.ts b/bluebird/bluebird-tests.ts index 440743c03..e4f3ee62e 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; // - - - - - - - - - - - - - - - - - @@ -237,36 +238,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; }); diff --git a/bluebird/bluebird.d.ts b/bluebird/bluebird.d.ts index 67067dc5b..48f56b248 100644 --- a/bluebird/bluebird.d.ts +++ b/bluebird/bluebird.d.ts @@ -33,11 +33,11 @@ declare class Promise implements Promise.Thenable, Promise.Inspection { * * 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. From f90f7cb4e08346934a9d4b2a9ce0ca959a948ef0 Mon Sep 17 00:00:00 2001 From: Eric Nicholson Date: Wed, 11 Nov 2015 08:45:47 -0500 Subject: [PATCH 3/3] Promise.then definitions that allow void in the error handler --- bluebird/bluebird-tests.ts | 15 +++++++++++++++ bluebird/bluebird.d.ts | 10 +++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/bluebird/bluebird-tests.ts b/bluebird/bluebird-tests.ts index e4f3ee62e..bd4f46fc4 100644 --- a/bluebird/bluebird-tests.ts +++ b/bluebird/bluebird-tests.ts @@ -227,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; }); diff --git a/bluebird/bluebird.d.ts b/bluebird/bluebird.d.ts index 48f56b248..9f36cf5bc 100644 --- a/bluebird/bluebird.d.ts +++ b/bluebird/bluebird.d.ts @@ -25,9 +25,9 @@ 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. * @@ -672,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 {