51 lines
No EOL
2.3 KiB
JavaScript
51 lines
No EOL
2.3 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* 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.retryAction = void 0;
|
|
/**
|
|
* Retry a given promise with gradually increasing delay when the HTTP status code received is 429(Too Many Requests).
|
|
*
|
|
* @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 retryAction(promise, maxRetries, initialDelay = 500) {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
let delay = initialDelay, n = 1;
|
|
let errStatusCode;
|
|
const errorsArray = [];
|
|
// Take care of negative or zero
|
|
maxRetries = Math.max(maxRetries, 1);
|
|
do {
|
|
try {
|
|
// Note: return await intentional so we can catch errors
|
|
return yield promise(n);
|
|
}
|
|
catch (err) {
|
|
errorsArray.push(err);
|
|
errStatusCode = err.statusCode;
|
|
if (err.statusCode == 429) {
|
|
yield new Promise((resolve) => setTimeout(resolve, delay));
|
|
delay *= n;
|
|
n++;
|
|
}
|
|
}
|
|
} while (n <= maxRetries && errStatusCode === 429);
|
|
throw { errors: errorsArray, message: 'Failed to perform the required operation.' };
|
|
});
|
|
}
|
|
exports.retryAction = retryAction;
|
|
//# sourceMappingURL=retryAction.js.map
|