microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/promise.ts
65lines · 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 | /** |
| 5 | * Utilities for working with promises. |
| 6 | */ |
| 7 | export class PromiseUtil { |
| 8 | public forEach<T>(sources: T[], promiseGenerator: (source: T) => Promise<void>): Promise<void> { |
| 9 | return Promise.all(sources.map(source => { |
| 10 | return promiseGenerator(source); |
| 11 | })).then(() => { }); |
| 12 | } |
| 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 | */ |
| 24 | public retryAsync<T>(operation: () => Promise<T>, condition: (result: T) => boolean | Promise<boolean>, maxRetries: number, delay: number, failure: string): Promise<T> { |
| 25 | return this.retryAsyncIteration(operation, condition, maxRetries, 0, delay, failure); |
| 26 | } |
| 27 | |
| 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()); |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | public static async delay(duration: number): Promise<void> { |
| 45 | return new Promise<void>(resolve => setTimeout(resolve, duration)); |
| 46 | } |
| 47 | |
| 48 | private retryAsyncIteration<T>(operation: () => Promise<T>, condition: (result: T) => boolean | Promise<boolean>, maxRetries: number, iteration: number, delay: number, failure: string): Promise<T> { |
| 49 | return operation() |
| 50 | .then(result => { |
| 51 | return Promise.resolve(result).then(condition).then((conditionResult => { |
| 52 | |
| 53 | if (conditionResult) { |
| 54 | return result; |
| 55 | } |
| 56 | |
| 57 | if (iteration < maxRetries) { |
| 58 | return PromiseUtil.delay(delay).then(() => this.retryAsyncIteration(operation, condition, maxRetries, iteration + 1, delay, failure)); |
| 59 | } |
| 60 | |
| 61 | throw new Error(failure); |
| 62 | })); |
| 63 | }); |
| 64 | } |
| 65 | } |