From eacef69706705ea2c8d2f7ca88edc5659d865d98 Mon Sep 17 00:00:00 2001 From: anchann Date: Fri, 16 Aug 2013 07:42:33 +0900 Subject: [PATCH] Q: adding inspect() method, changing return type of getSettled As per documentation: https://github.com/kriskowal/q/wiki/API-Reference#promiseallsettled https://github.com/kriskowal/q/wiki/API-Reference#promiseinspect --- q/Q-tests.ts | 17 ++++++++++++++++- q/Q.d.ts | 14 +++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/q/Q-tests.ts b/q/Q-tests.ts index fea025b1c8..3407259ca0 100644 --- a/q/Q-tests.ts +++ b/q/Q-tests.ts @@ -90,4 +90,19 @@ Q.all(myNums).then(nums => nums.map(Math.round)); Q.fbind((dateString) => new Date(dateString), "11/11/1991")().then(d => d.toLocaleDateString()); Q.when(8, num => num + "!"); -Q.when(Q(8), num => num + "!").then(str => str.split(',')); \ No newline at end of file +Q.when(Q(8), num => num + "!").then(str => str.split(',')); + +declare function saveToDisk(): Q.Promise; +declare function saveToCloud(): Q.Promise; +Q.allSettled([saveToDisk(), saveToCloud()]).spread(function (disk, cloud) { + console.log("saved to disk:", disk.state === "fulfilled"); + console.log("saved to cloud:", cloud.state === "fulfilled"); + + if (disk.state === "fulfilled") { + console.log("value was " + disk.value); + } + else if (disk.state === "rejected") { + console.log("rejected because " + disk.reason); + } +}).done(); + diff --git a/q/Q.d.ts b/q/Q.d.ts index 4dc20fb375..a8b4b3e335 100644 --- a/q/Q.d.ts +++ b/q/Q.d.ts @@ -60,6 +60,14 @@ declare module Q { isPending(): boolean; valueOf(): any; + + inspect(): PromiseState; + } + + interface PromiseState { + state: string /* "fulfilled", "rejected", "pending" */; + value?: T; + reason?: any; } // if no fulfill, reject, or progress provided, returned promise will be of same type @@ -96,8 +104,8 @@ declare module Q { export function all(promises: any[]): Promise; export function all(promises: IPromise[]): Promise; - export function allSettled(promises: any[]): Promise[]>; - export function allSettled(promises: IPromise[]): Promise[]>; + export function allSettled(promises: any[]): Promise[]>; + export function allSettled(promises: IPromise[]): Promise[]>; export function allResolved(promises: any[]): Promise[]>; export function allResolved(promises: IPromise[]): Promise[]>; @@ -145,4 +153,4 @@ declare module Q { declare module "q" { export = Q; -} \ No newline at end of file +}