microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/promise.ts
107lines · 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> { |
34472878RedMickey5 years ago | 9 | return Promise.all( |
| 10 | sources.map(source => { | |
52f3873ddigeff10 years ago | 11 | return promiseGenerator(source); |
34472878RedMickey5 years ago | 12 | }), |
| 13 | ).then(() => {}); // eslint-disable-line @typescript-eslint/no-empty-function | |
52f3873ddigeff10 years ago | 14 | } |
2f10b3adunknown10 years ago | 15 | /** |
| 16 | * Retries an operation a given number of times. For each retry, a condition is checked. | |
| 17 | * If the condition is not satisfied after the maximum number of retries, and error is thrown. | |
| 18 | * Otherwise, the result of the operation is returned once the condition is satisfied. | |
| 19 | * | |
| 20 | * @param operation - the function to execute. | |
| 21 | * @param condition - the condition to check between iterations. | |
| 22 | * @param maxRetries - the maximum number of retries. | |
| 23 | * @param delay - time between iterations, in milliseconds. | |
| 24 | * @param failure - error description. | |
| 25 | */ | |
34472878RedMickey5 years ago | 26 | public retryAsync<T>( |
| 27 | operation: () => Promise<T>, | |
| 28 | condition: (result: T) => boolean | Promise<boolean>, | |
| 29 | maxRetries: number, | |
| 30 | delay: number, | |
| 31 | failure: string, | |
| 32 | ): Promise<T> { | |
2f10b3adunknown10 years ago | 33 | return this.retryAsyncIteration(operation, condition, maxRetries, 0, delay, failure); |
| 34 | } | |
| 35 | | |
34472878RedMickey5 years ago | 36 | public reduce<T>( |
| 37 | sources: T[] | Promise<T[]>, | |
| 38 | generateAsyncOperation: (value: T) => Promise<void>, | |
| 39 | ): Promise<void> { | |
ce5e88eeYuri Skorokhodov5 years ago | 40 | let promisedSources: Promise<T[]>; |
| 41 | if (sources instanceof Promise) { | |
| 42 | promisedSources = sources; | |
| 43 | } else { | |
| 44 | promisedSources = Promise.resolve(sources); | |
| 45 | } | |
34472878RedMickey5 years ago | 46 | return promisedSources.then(resolvedSources => { |
| 47 | return resolvedSources.reduce((previousReduction: Promise<void>, newSource: T) => { | |
| 48 | return previousReduction.then(() => { | |
| 49 | return generateAsyncOperation(newSource); | |
| 50 | }); | |
| 51 | }, Promise.resolve()); | |
bc6696cbdigeff10 years ago | 52 | }); |
| 53 | } | |
| 54 | | |
259c018fYuri Skorokhodov5 years ago | 55 | public static async delay(duration: number): Promise<void> { |
d9c9ddcbRedMickey6 years ago | 56 | return new Promise<void>(resolve => setTimeout(resolve, duration)); |
| 57 | } | |
| 58 | | |
4bb0956eRedMickey5 years ago | 59 | public static promiseCacheDecorator<T>( |
| 60 | func: (...args: any[]) => Promise<T>, | |
| 61 | context: Record<string, any> | null = null, | |
| 62 | ): (...args: any[]) => Promise<T> { | |
| 63 | let promise: Promise<T>; | |
| 64 | return (...args: any[]): Promise<T> => { | |
| 65 | if (promise) { | |
| 66 | return promise; | |
| 67 | } else { | |
| 68 | promise = func.apply(context, args); | |
| 69 | return promise; | |
| 70 | } | |
| 71 | }; | |
| 72 | } | |
| 73 | | |
34472878RedMickey5 years ago | 74 | private retryAsyncIteration<T>( |
| 75 | operation: () => Promise<T>, | |
| 76 | condition: (result: T) => boolean | Promise<boolean>, | |
| 77 | maxRetries: number, | |
| 78 | iteration: number, | |
| 79 | delay: number, | |
| 80 | failure: string, | |
| 81 | ): Promise<T> { | |
| 82 | return operation().then(result => { | |
| 83 | return Promise.resolve(result) | |
| 84 | .then(condition) | |
| 85 | .then(conditionResult => { | |
b7451aefRedMickey6 years ago | 86 | if (conditionResult) { |
| 87 | return result; | |
| 88 | } | |
| 89 | | |
| 90 | if (iteration < maxRetries) { | |
34472878RedMickey5 years ago | 91 | return PromiseUtil.delay(delay).then(() => |
| 92 | this.retryAsyncIteration( | |
| 93 | operation, | |
| 94 | condition, | |
| 95 | maxRetries, | |
| 96 | iteration + 1, | |
| 97 | delay, | |
| 98 | failure, | |
| 99 | ), | |
| 100 | ); | |
b7451aefRedMickey6 years ago | 101 | } |
| 102 | | |
| 103 | throw new Error(failure); | |
34472878RedMickey5 years ago | 104 | }); |
| 105 | }); | |
b7451aefRedMickey6 years ago | 106 | } |
34472878RedMickey5 years ago | 107 | } |