microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.5.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/promise.ts

107lines · 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
4/**
5* Utilities for working with promises.
6*/
34ec0375Daniel10 years ago7export class PromiseUtil {
ce5e88eeYuri Skorokhodov5 years ago8public forEach<T>(sources: T[], promiseGenerator: (source: T) => Promise<void>): Promise<void> {
34472878RedMickey5 years ago9return Promise.all(
10sources.map(source => {
52f3873ddigeff10 years ago11return promiseGenerator(source);
34472878RedMickey5 years ago12}),
13).then(() => {}); // eslint-disable-line @typescript-eslint/no-empty-function
52f3873ddigeff10 years ago14}
2f10b3adunknown10 years ago15/**
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 ago26public retryAsync<T>(
27operation: () => Promise<T>,
28condition: (result: T) => boolean | Promise<boolean>,
29maxRetries: number,
30delay: number,
31failure: string,
32): Promise<T> {
2f10b3adunknown10 years ago33return this.retryAsyncIteration(operation, condition, maxRetries, 0, delay, failure);
34}
35
34472878RedMickey5 years ago36public reduce<T>(
37sources: T[] | Promise<T[]>,
38generateAsyncOperation: (value: T) => Promise<void>,
39): Promise<void> {
ce5e88eeYuri Skorokhodov5 years ago40let promisedSources: Promise<T[]>;
41if (sources instanceof Promise) {
42promisedSources = sources;
43} else {
44promisedSources = Promise.resolve(sources);
45}
34472878RedMickey5 years ago46return promisedSources.then(resolvedSources => {
47return resolvedSources.reduce((previousReduction: Promise<void>, newSource: T) => {
48return previousReduction.then(() => {
49return generateAsyncOperation(newSource);
50});
51}, Promise.resolve());
bc6696cbdigeff10 years ago52});
53}
54
259c018fYuri Skorokhodov5 years ago55public static async delay(duration: number): Promise<void> {
d9c9ddcbRedMickey6 years ago56return new Promise<void>(resolve => setTimeout(resolve, duration));
57}
58
4bb0956eRedMickey5 years ago59public static promiseCacheDecorator<T>(
60func: (...args: any[]) => Promise<T>,
61context: Record<string, any> | null = null,
62): (...args: any[]) => Promise<T> {
63let promise: Promise<T>;
64return (...args: any[]): Promise<T> => {
65if (promise) {
66return promise;
67} else {
68promise = func.apply(context, args);
69return promise;
70}
71};
72}
73
34472878RedMickey5 years ago74private retryAsyncIteration<T>(
75operation: () => Promise<T>,
76condition: (result: T) => boolean | Promise<boolean>,
77maxRetries: number,
78iteration: number,
79delay: number,
80failure: string,
81): Promise<T> {
82return operation().then(result => {
83return Promise.resolve(result)
84.then(condition)
85.then(conditionResult => {
b7451aefRedMickey6 years ago86if (conditionResult) {
87return result;
88}
89
90if (iteration < maxRetries) {
34472878RedMickey5 years ago91return PromiseUtil.delay(delay).then(() =>
92this.retryAsyncIteration(
93operation,
94condition,
95maxRetries,
96iteration + 1,
97delay,
98failure,
99),
100);
b7451aefRedMickey6 years ago101}
102
103throw new Error(failure);
34472878RedMickey5 years ago104});
105});
b7451aefRedMickey6 years ago106}
34472878RedMickey5 years ago107}