microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
bcb44c4d4e94b00d16fc644eee9a1d64a2ef86b7

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/common/node/promise.ts

92lines · 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 */
7export 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 private retryAsyncIteration<T>(
60 operation: () => Promise<T>,
61 condition: (result: T) => boolean | Promise<boolean>,
62 maxRetries: number,
63 iteration: number,
64 delay: number,
65 failure: string,
66 ): Promise<T> {
67 return operation().then(result => {
68 return Promise.resolve(result)
69 .then(condition)
70 .then(conditionResult => {
71 if (conditionResult) {
72 return result;
73 }
74
75 if (iteration < maxRetries) {
76 return PromiseUtil.delay(delay).then(() =>
77 this.retryAsyncIteration(
78 operation,
79 condition,
80 maxRetries,
81 iteration + 1,
82 delay,
83 failure,
84 ),
85 );
86 }
87
88 throw new Error(failure);
89 });
90 });
91 }
92}