From 9cbb16d81e20e670b6b15e157b46fb3ef8a78286 Mon Sep 17 00:00:00 2001 From: d-ph Date: Sat, 1 Nov 2014 10:57:08 +0000 Subject: [PATCH] Fix .fail<> generic precedence bug and missing generic on .reject() --- q/Q-tests.ts | 48 +++++++++++++++++++++++++++++++++++++++++++++++- q/Q.d.ts | 4 ++-- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/q/Q-tests.ts b/q/Q-tests.ts index 48a3224bc1..b5f03a3fd8 100644 --- a/q/Q-tests.ts +++ b/q/Q-tests.ts @@ -137,4 +137,50 @@ class Repo { } var kitty = new Repo(); -Q.nbind(kitty.find, kitty)({ cute: true }).done((kitties: any[]) => {}); \ No newline at end of file +Q.nbind(kitty.find, kitty)({ cute: true }).done((kitties: any[]) => {}); + + +/* + * Test: Can "rethrow" rejected promises + */ +module TestCanRethrowRejectedPromises { + + interface Foo { + a: number; + } + + function nestedBar(): Q.Promise { + var deferred = Q.defer(); + + return deferred.promise; + } + + function bar(): Q.Promise { + return nestedBar() + .then((foo:Foo) => { + console.log("Lorem ipsum"); + }) + .fail((error) => { + console.log("Intermediate error handling"); + + /* + * Cannot do this, because: + * error TS2322: Type 'Promise' is not assignable to type 'Promise' + */ + //throw error; + + return Q.reject(error); + }) + ; + } + + bar() + .finally(() => { + console.log("Cleanup") + }) + .done() + ; + +} + + diff --git a/q/Q.d.ts b/q/Q.d.ts index 3e7371ead2..b43f516f6e 100644 --- a/q/Q.d.ts +++ b/q/Q.d.ts @@ -74,8 +74,8 @@ declare module Q { */ spread(onFulfilled: Function, onRejected?: Function): Promise; - fail(onRejected: (reason: any) => U): Promise; fail(onRejected: (reason: any) => IPromise): Promise; + fail(onRejected: (reason: any) => U): Promise; /** * A sugar method, equivalent to promise.then(undefined, onRejected). */ @@ -329,7 +329,7 @@ declare module Q { /** * Returns a promise that is rejected with reason. */ - export function reject(reason?: any): Promise; + export function reject(reason?: any): Promise; export function Promise(resolver: (resolve: (val: IPromise) => void , reject: (reason: any) => void , notify: (progress: any) => void ) => void ): Promise; export function Promise(resolver: (resolve: (val: T) => void , reject: (reason: any) => void , notify: (progress: any) => void ) => void ): Promise;