From 8e76ee411e538cca53544b8621986214e847e132 Mon Sep 17 00:00:00 2001 From: Alessandro Vergani Date: Thu, 23 Feb 2017 10:51:56 +0100 Subject: [PATCH] Add missing catchReturn and catchThrow --- bluebird-global/index.d.ts | 2 ++ bluebird/bluebird-tests.ts | 9 +++++++++ bluebird/index.d.ts | 31 +++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/bluebird-global/index.d.ts b/bluebird-global/index.d.ts index e4c4920501..a705222e9d 100644 --- a/bluebird-global/index.d.ts +++ b/bluebird-global/index.d.ts @@ -80,6 +80,8 @@ declare global { // then: typeof Bluebird.prototype.then; thenReturn: typeof Bluebird.prototype.thenReturn; thenThrow: typeof Bluebird.prototype.thenThrow; + catchReturn: typeof Bluebird.prototype.catchReturn; + catchThrow: typeof Bluebird.prototype.catchThrow; throw: typeof Bluebird.prototype.throw; timeout: typeof Bluebird.prototype.timeout; toJSON: typeof Bluebird.prototype.toJSON; diff --git a/bluebird/bluebird-tests.ts b/bluebird/bluebird-tests.ts index c08a7bdc6b..b9fc8d0cef 100644 --- a/bluebird/bluebird-tests.ts +++ b/bluebird/bluebird-tests.ts @@ -506,6 +506,15 @@ fooProm = fooProm.thenThrow(err); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +barProm = fooProm.catchReturn(bar); + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +// fooProm +fooProm = fooProm.catchThrow(err); + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + str = fooProm.toString(); obj = fooProm.toJSON(); diff --git a/bluebird/index.d.ts b/bluebird/index.d.ts index ecacedfd0d..3508a4e742 100644 --- a/bluebird/index.d.ts +++ b/bluebird/index.d.ts @@ -248,6 +248,37 @@ declare class Bluebird implements Bluebird.Thenable, Bluebird.Inspection; thenThrow(reason: Error): Bluebird; + /** + * Convenience method for: + * + * + * .catch(function() { + * return value; + * }); + * + * + * in the case where `value` doesn't change its value. That means `value` is bound at the time of calling `.catchReturn()` + */ + catchReturn(value: U): Bluebird; + catchReturn(predicate: (error: any) => boolean, value: U): Bluebird; + catchReturn(ErrorClass: new (...args: any[]) => E, value: U): Bluebird; + catchReturn(predicate: Object, value: U): Bluebird; + + /** + * Convenience method for: + * + * + * .catch(function() { + * throw reason; + * }); + * + * Same limitations apply as with `.catchReturn()`. + */ + catchThrow(reason: Error): Bluebird; + catchThrow(predicate: (error: any) => boolean, reason: Error): Bluebird; + catchThrow(ErrorClass: new (...args: any[]) => E, reason: Error): Bluebird; + catchThrow(predicate: Object, reason: Error): Bluebird; + /** * Convert to String. */