microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/promise.ts
65lines · 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 | | |
| 4 | /** | |
| 5 | * Utilities for working with promises. | |
| 6 | */ | |
34ec0375Daniel10 years ago | 7 | export class PromiseUtil { |
ce5e88eeYuri Skorokhodov5 years ago | 8 | public forEach<T>(sources: T[], promiseGenerator: (source: T) => Promise<void>): Promise<void> { |
| 9 | return Promise.all(sources.map(source => { | |
52f3873ddigeff10 years ago | 10 | return promiseGenerator(source); |
| 11 | })).then(() => { }); | |
| 12 | } | |
2f10b3adunknown10 years ago | 13 | /** |
| 14 | * Retries an operation a given number of times. For each retry, a condition is checked. | |
| 15 | * If the condition is not satisfied after the maximum number of retries, and error is thrown. | |
| 16 | * Otherwise, the result of the operation is returned once the condition is satisfied. | |
| 17 | * | |
| 18 | * @param operation - the function to execute. | |
| 19 | * @param condition - the condition to check between iterations. | |
| 20 | * @param maxRetries - the maximum number of retries. | |
| 21 | * @param delay - time between iterations, in milliseconds. | |
| 22 | * @param failure - error description. | |
| 23 | */ | |
ce5e88eeYuri Skorokhodov5 years ago | 24 | public retryAsync<T>(operation: () => Promise<T>, condition: (result: T) => boolean | Promise<boolean>, maxRetries: number, delay: number, failure: string): Promise<T> { |
2f10b3adunknown10 years ago | 25 | return this.retryAsyncIteration(operation, condition, maxRetries, 0, delay, failure); |
| 26 | } | |
| 27 | | |
ce5e88eeYuri Skorokhodov5 years ago | 28 | public reduce<T>(sources: T[] | Promise<T[]>, generateAsyncOperation: (value: T) => Promise<void>): Promise<void> { |
| 29 | let promisedSources: Promise<T[]>; | |
| 30 | if (sources instanceof Promise) { | |
| 31 | promisedSources = sources; | |
| 32 | } else { | |
| 33 | promisedSources = Promise.resolve(sources); | |
| 34 | } | |
| 35 | return promisedSources.then((resolvedSources) => { | |
| 36 | return resolvedSources.reduce((previousReduction: Promise<void>, newSource: T) => { | |
| 37 | return previousReduction.then(() => { | |
| 38 | return generateAsyncOperation(newSource); | |
| 39 | }); | |
| 40 | }, Promise.resolve()); | |
bc6696cbdigeff10 years ago | 41 | }); |
| 42 | } | |
| 43 | | |
259c018fYuri Skorokhodov5 years ago | 44 | public static async delay(duration: number): Promise<void> { |
d9c9ddcbRedMickey6 years ago | 45 | return new Promise<void>(resolve => setTimeout(resolve, duration)); |
| 46 | } | |
| 47 | | |
ce5e88eeYuri Skorokhodov5 years ago | 48 | private retryAsyncIteration<T>(operation: () => Promise<T>, condition: (result: T) => boolean | Promise<boolean>, maxRetries: number, iteration: number, delay: number, failure: string): Promise<T> { |
b7451aefRedMickey6 years ago | 49 | return operation() |
| 50 | .then(result => { | |
| 51 | return Promise.resolve(result).then(condition).then((conditionResult => { | |
ce5e88eeYuri Skorokhodov5 years ago | 52 | |
b7451aefRedMickey6 years ago | 53 | if (conditionResult) { |
| 54 | return result; | |
| 55 | } | |
| 56 | | |
| 57 | if (iteration < maxRetries) { | |
259c018fYuri Skorokhodov5 years ago | 58 | return PromiseUtil.delay(delay).then(() => this.retryAsyncIteration(operation, condition, maxRetries, iteration + 1, delay, failure)); |
b7451aefRedMickey6 years ago | 59 | } |
| 60 | | |
| 61 | throw new Error(failure); | |
| 62 | })); | |
| 63 | }); | |
| 64 | } | |
2f10b3adunknown10 years ago | 65 | } |