46 lines
No EOL
2 KiB
JavaScript
46 lines
No EOL
2 KiB
JavaScript
"use strict";
|
|
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.retry = void 0;
|
|
/**
|
|
* Retry a given promise with gradually increasing delay.
|
|
*
|
|
* @param promise a function that returns a promise to retry
|
|
* @param maxRetries the maximum number of times to retry
|
|
* @param initialDelay the initial value to delay before retrying (in milliseconds)
|
|
* @returns a promise resolving to the result of the promise from the promise generating function, or undefined
|
|
*/
|
|
function retry(promise, maxRetries, initialDelay = 500) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
let delay = initialDelay, n = 1, maybeError;
|
|
// Take care of negative or zero
|
|
maxRetries = Math.max(maxRetries, 1);
|
|
while (n <= maxRetries) {
|
|
try {
|
|
// Note: return await intentional so we can catch errors
|
|
return yield promise(n);
|
|
}
|
|
catch (err) {
|
|
maybeError = err;
|
|
yield new Promise((resolve) => setTimeout(resolve, delay));
|
|
delay *= n;
|
|
n++;
|
|
}
|
|
}
|
|
if (maybeError) {
|
|
throw maybeError;
|
|
}
|
|
});
|
|
}
|
|
exports.retry = retry;
|
|
//# sourceMappingURL=retry.js.map
|