microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.3.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/node/promise.ts

61lines · 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
a116786bJimmy Thomson10 years ago4import * as Q from "q";
5
2f10b3adunknown10 years ago6/**
7* Utilities for working with promises.
8*/
34ec0375Daniel10 years ago9export class PromiseUtil {
52f3873ddigeff10 years ago10public forEach<T>(sourcesMaybePromise: Q.Promise<T[]> | T[], promiseGenerator: (source: T) => Q.Promise<void>): Q.Promise<void> {
11const sourcesPromise = <Q.Promise<T[]>>Q(sourcesMaybePromise);
12return Q(sourcesPromise).then(sources => {
13return Q.all(sources.map(source => {
14return promiseGenerator(source);
15})).then(() => { });
16});
17}
18
2f10b3adunknown10 years ago19/**
20* Retries an operation a given number of times. For each retry, a condition is checked.
21* If the condition is not satisfied after the maximum number of retries, and error is thrown.
22* Otherwise, the result of the operation is returned once the condition is satisfied.
23*
24* @param operation - the function to execute.
25* @param condition - the condition to check between iterations.
26* @param maxRetries - the maximum number of retries.
27* @param delay - time between iterations, in milliseconds.
28* @param failure - error description.
29*/
a116786bJimmy Thomson10 years ago30public retryAsync<T>(operation: () => Q.Promise<T>, condition: (result: T) => boolean | Q.Promise<boolean>, maxRetries: number, delay: number, failure: string): Q.Promise<T> {
2f10b3adunknown10 years ago31return this.retryAsyncIteration(operation, condition, maxRetries, 0, delay, failure);
32}
33
bc6696cbdigeff10 years ago34public reduce<T>(sources: T[]|Q.Promise<T[]>, generateAsyncOperation: (value: T) => Q.Promise<void>): Q.Promise<void> {
35const promisedSources = <Q.Promise<T[]>>Q(sources);
36return promisedSources.then(resolvedSources => {
37return resolvedSources.reduce((previousReduction: Q.Promise<void>, newSource: T) => {
38return previousReduction.then(() => {
39return generateAsyncOperation(newSource);
40});
41}, Q<void>(void 0));
42});
43}
44
a116786bJimmy Thomson10 years ago45private retryAsyncIteration<T>(operation: () => Q.Promise<T>, condition: (result: T) => boolean | Q.Promise<boolean>, maxRetries: number, iteration: number, delay: number, failure: string): Q.Promise<T> {
2f10b3adunknown10 years ago46return operation()
47.then(result => {
48return Q(result).then(condition).then((conditionResult => {
49if (conditionResult) {
50return result;
51}
52
53if (iteration < maxRetries) {
54return Q.delay(delay).then(() => this.retryAsyncIteration(operation, condition, maxRetries, iteration + 1, delay, failure));
55}
56
57throw new Error(failure);
58}));
59});
60}
61}