microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/node/promise.ts
107lines · 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( |
| 10 | sources.map(source => { |
| 11 | return promiseGenerator(source); |
| 12 | }), |
| 13 | ).then(() => {}); // eslint-disable-line @typescript-eslint/no-empty-function |
| 14 | } |
| 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 | */ |
| 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> { |
| 33 | return this.retryAsyncIteration(operation, condition, maxRetries, 0, delay, failure); |
| 34 | } |
| 35 | |
| 36 | public reduce<T>( |
| 37 | sources: T[] | Promise<T[]>, |
| 38 | generateAsyncOperation: (value: T) => Promise<void>, |
| 39 | ): Promise<void> { |
| 40 | let promisedSources: Promise<T[]>; |
| 41 | if (sources instanceof Promise) { |
| 42 | promisedSources = sources; |
| 43 | } else { |
| 44 | promisedSources = Promise.resolve(sources); |
| 45 | } |
| 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()); |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | public static async delay(duration: number): Promise<void> { |
| 56 | return new Promise<void>(resolve => setTimeout(resolve, duration)); |
| 57 | } |
| 58 | |
| 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 | |
| 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 => { |
| 86 | if (conditionResult) { |
| 87 | return result; |
| 88 | } |
| 89 | |
| 90 | if (iteration < maxRetries) { |
| 91 | return PromiseUtil.delay(delay).then(() => |
| 92 | this.retryAsyncIteration( |
| 93 | operation, |
| 94 | condition, |
| 95 | maxRetries, |
| 96 | iteration + 1, |
| 97 | delay, |
| 98 | failure, |
| 99 | ), |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | throw new Error(failure); |
| 104 | }); |
| 105 | }); |
| 106 | } |
| 107 | } |
| 108 | |