microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.8.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/promise.ts

117lines · modeblame

2f10b3adunknown10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
f338085detatanova4 years ago4import { CancellationTokenSource } from "vscode";
5
2f10b3adunknown10 years ago6/**
7* Utilities for working with promises.
8*/
34ec0375Daniel10 years ago9export class PromiseUtil {
0d77292aJiglioNero4 years ago10public static async forEach<T>(
11sources: T[],
12promiseGenerator: (source: T) => Promise<void>,
13): Promise<void> {
14await Promise.all(
34472878RedMickey5 years ago15sources.map(source => {
52f3873ddigeff10 years ago16return promiseGenerator(source);
34472878RedMickey5 years ago17}),
0d77292aJiglioNero4 years ago18);
52f3873ddigeff10 years ago19}
2f10b3adunknown10 years ago20/**
21* Retries an operation a given number of times. For each retry, a condition is checked.
22* If the condition is not satisfied after the maximum number of retries, and error is thrown.
23* Otherwise, the result of the operation is returned once the condition is satisfied.
24*
25* @param operation - the function to execute.
26* @param condition - the condition to check between iterations.
27* @param maxRetries - the maximum number of retries.
28* @param delay - time between iterations, in milliseconds.
29* @param failure - error description.
30*/
0d77292aJiglioNero4 years ago31public static retryAsync<T>(
34472878RedMickey5 years ago32operation: () => Promise<T>,
33condition: (result: T) => boolean | Promise<boolean>,
34maxRetries: number,
35delay: number,
36failure: string,
f338085detatanova4 years ago37cancellationTokenSource?: CancellationTokenSource,
34472878RedMickey5 years ago38): Promise<T> {
f338085detatanova4 years ago39return this.retryAsyncIteration(
40operation,
41condition,
42maxRetries,
430,
44delay,
45failure,
46cancellationTokenSource,
47);
2f10b3adunknown10 years ago48}
49
0d77292aJiglioNero4 years ago50public static async reduce<T>(
34472878RedMickey5 years ago51sources: T[] | Promise<T[]>,
52generateAsyncOperation: (value: T) => Promise<void>,
53): Promise<void> {
0d77292aJiglioNero4 years ago54let arraySources: T[];
ce5e88eeYuri Skorokhodov5 years ago55if (sources instanceof Promise) {
0d77292aJiglioNero4 years ago56arraySources = await sources;
ce5e88eeYuri Skorokhodov5 years ago57} else {
0d77292aJiglioNero4 years ago58arraySources = sources;
ce5e88eeYuri Skorokhodov5 years ago59}
0d77292aJiglioNero4 years ago60
61return arraySources.reduce(async (previousReduction: Promise<void>, newSource: T) => {
62await previousReduction;
63return generateAsyncOperation(newSource);
64}, Promise.resolve());
bc6696cbdigeff10 years ago65}
66
259c018fYuri Skorokhodov5 years ago67public static async delay(duration: number): Promise<void> {
d9c9ddcbRedMickey6 years ago68return new Promise<void>(resolve => setTimeout(resolve, duration));
69}
70
4bb0956eRedMickey5 years ago71public static promiseCacheDecorator<T>(
72func: (...args: any[]) => Promise<T>,
73context: Record<string, any> | null = null,
74): (...args: any[]) => Promise<T> {
75let promise: Promise<T>;
76return (...args: any[]): Promise<T> => {
0d77292aJiglioNero4 years ago77if (!promise) {
4bb0956eRedMickey5 years ago78promise = func.apply(context, args);
79}
0d77292aJiglioNero4 years ago80return promise;
4bb0956eRedMickey5 years ago81};
82}
83
0d77292aJiglioNero4 years ago84private static async retryAsyncIteration<T>(
34472878RedMickey5 years ago85operation: () => Promise<T>,
86condition: (result: T) => boolean | Promise<boolean>,
87maxRetries: number,
88iteration: number,
89delay: number,
90failure: string,
f338085detatanova4 years ago91cancellationTokenSource?: CancellationTokenSource,
34472878RedMickey5 years ago92): Promise<T> {
0d77292aJiglioNero4 years ago93const result = await operation();
94const conditionResult = await condition(result);
95if (conditionResult) {
96return result;
97}
b7451aefRedMickey6 years ago98
0d77292aJiglioNero4 years ago99if (
100iteration < maxRetries &&
101!(cancellationTokenSource && cancellationTokenSource.token.isCancellationRequested)
102) {
103await PromiseUtil.delay(delay);
104return this.retryAsyncIteration(
105operation,
106condition,
107maxRetries,
108iteration + 1,
109delay,
110failure,
111cancellationTokenSource,
112);
113}
b7451aefRedMickey6 years ago114
0d77292aJiglioNero4 years ago115throw new Error(failure);
b7451aefRedMickey6 years ago116}
34472878RedMickey5 years ago117}