microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/promise.ts
61lines · modeblame
2f10b3adunknown10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
a116786bJimmy Thomson10 years ago | 4 | import * as Q from "q"; |
| 5 | | |
2f10b3adunknown10 years ago | 6 | /** |
| 7 | * Utilities for working with promises. | |
| 8 | */ | |
34ec0375Daniel10 years ago | 9 | export class PromiseUtil { |
52f3873ddigeff10 years ago | 10 | public forEach<T>(sourcesMaybePromise: Q.Promise<T[]> | T[], promiseGenerator: (source: T) => Q.Promise<void>): Q.Promise<void> { |
| 11 | const sourcesPromise = <Q.Promise<T[]>>Q(sourcesMaybePromise); | |
| 12 | return Q(sourcesPromise).then(sources => { | |
| 13 | return Q.all(sources.map(source => { | |
| 14 | return promiseGenerator(source); | |
| 15 | })).then(() => { }); | |
| 16 | }); | |
| 17 | } | |
| 18 | | |
2f10b3adunknown10 years ago | 19 | /** |
| 20 | * Retries an operation a given number of times. For each retry, a condition is checked. | |
| 21 | * If the condition is not satisfied after the maximum number of retries, and error is thrown. | |
| 22 | * Otherwise, the result of the operation is returned once the condition is satisfied. | |
| 23 | * | |
| 24 | * @param operation - the function to execute. | |
| 25 | * @param condition - the condition to check between iterations. | |
| 26 | * @param maxRetries - the maximum number of retries. | |
| 27 | * @param delay - time between iterations, in milliseconds. | |
| 28 | * @param failure - error description. | |
| 29 | */ | |
a116786bJimmy Thomson10 years ago | 30 | public retryAsync<T>(operation: () => Q.Promise<T>, condition: (result: T) => boolean | Q.Promise<boolean>, maxRetries: number, delay: number, failure: string): Q.Promise<T> { |
2f10b3adunknown10 years ago | 31 | return this.retryAsyncIteration(operation, condition, maxRetries, 0, delay, failure); |
| 32 | } | |
| 33 | | |
bc6696cbdigeff10 years ago | 34 | public reduce<T>(sources: T[]|Q.Promise<T[]>, generateAsyncOperation: (value: T) => Q.Promise<void>): Q.Promise<void> { |
| 35 | const promisedSources = <Q.Promise<T[]>>Q(sources); | |
| 36 | return promisedSources.then(resolvedSources => { | |
| 37 | return resolvedSources.reduce((previousReduction: Q.Promise<void>, newSource: T) => { | |
| 38 | return previousReduction.then(() => { | |
| 39 | return generateAsyncOperation(newSource); | |
| 40 | }); | |
| 41 | }, Q<void>(void 0)); | |
| 42 | }); | |
| 43 | } | |
| 44 | | |
a116786bJimmy Thomson10 years ago | 45 | private retryAsyncIteration<T>(operation: () => Q.Promise<T>, condition: (result: T) => boolean | Q.Promise<boolean>, maxRetries: number, iteration: number, delay: number, failure: string): Q.Promise<T> { |
2f10b3adunknown10 years ago | 46 | return operation() |
| 47 | .then(result => { | |
| 48 | return Q(result).then(condition).then((conditionResult => { | |
| 49 | if (conditionResult) { | |
| 50 | return result; | |
| 51 | } | |
| 52 | | |
| 53 | if (iteration < maxRetries) { | |
| 54 | return Q.delay(delay).then(() => this.retryAsyncIteration(operation, condition, maxRetries, iteration + 1, delay, failure)); | |
| 55 | } | |
| 56 | | |
| 57 | throw new Error(failure); | |
| 58 | })); | |
| 59 | }); | |
| 60 | } | |
| 61 | } |