microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/promise.ts
61lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import * as Q from "q"; |
| 5 | |
| 6 | /** |
| 7 | * Utilities for working with promises. |
| 8 | */ |
| 9 | export class PromiseUtil { |
| 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 | |
| 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 | */ |
| 30 | public retryAsync<T>(operation: () => Q.Promise<T>, condition: (result: T) => boolean | Q.Promise<boolean>, maxRetries: number, delay: number, failure: string): Q.Promise<T> { |
| 31 | return this.retryAsyncIteration(operation, condition, maxRetries, 0, delay, failure); |
| 32 | } |
| 33 | |
| 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 | |
| 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> { |
| 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 | } |