microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.0.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/promise.ts

65lines · 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> {
9return Promise.all(sources.map(source => {
52f3873ddigeff10 years ago10return promiseGenerator(source);
11})).then(() => { });
12}
2f10b3adunknown10 years ago13/**
14* Retries an operation a given number of times. For each retry, a condition is checked.
15* If the condition is not satisfied after the maximum number of retries, and error is thrown.
16* Otherwise, the result of the operation is returned once the condition is satisfied.
17*
18* @param operation - the function to execute.
19* @param condition - the condition to check between iterations.
20* @param maxRetries - the maximum number of retries.
21* @param delay - time between iterations, in milliseconds.
22* @param failure - error description.
23*/
ce5e88eeYuri Skorokhodov5 years ago24public retryAsync<T>(operation: () => Promise<T>, condition: (result: T) => boolean | Promise<boolean>, maxRetries: number, delay: number, failure: string): Promise<T> {
2f10b3adunknown10 years ago25return this.retryAsyncIteration(operation, condition, maxRetries, 0, delay, failure);
26}
27
ce5e88eeYuri Skorokhodov5 years ago28public reduce<T>(sources: T[] | Promise<T[]>, generateAsyncOperation: (value: T) => Promise<void>): Promise<void> {
29let promisedSources: Promise<T[]>;
30if (sources instanceof Promise) {
31promisedSources = sources;
32} else {
33promisedSources = Promise.resolve(sources);
34}
35return promisedSources.then((resolvedSources) => {
36return resolvedSources.reduce((previousReduction: Promise<void>, newSource: T) => {
37return previousReduction.then(() => {
38return generateAsyncOperation(newSource);
39});
40}, Promise.resolve());
bc6696cbdigeff10 years ago41});
42}
43
259c018fYuri Skorokhodov5 years ago44public static async delay(duration: number): Promise<void> {
d9c9ddcbRedMickey6 years ago45return new Promise<void>(resolve => setTimeout(resolve, duration));
46}
47
ce5e88eeYuri Skorokhodov5 years ago48private retryAsyncIteration<T>(operation: () => Promise<T>, condition: (result: T) => boolean | Promise<boolean>, maxRetries: number, iteration: number, delay: number, failure: string): Promise<T> {
b7451aefRedMickey6 years ago49return operation()
50.then(result => {
51return Promise.resolve(result).then(condition).then((conditionResult => {
ce5e88eeYuri Skorokhodov5 years ago52
b7451aefRedMickey6 years ago53if (conditionResult) {
54return result;
55}
56
57if (iteration < maxRetries) {
259c018fYuri Skorokhodov5 years ago58return PromiseUtil.delay(delay).then(() => this.retryAsyncIteration(operation, condition, maxRetries, iteration + 1, delay, failure));
b7451aefRedMickey6 years ago59}
60
61throw new Error(failure);
62}));
63});
64}
2f10b3adunknown10 years ago65}