refactor(retry): simplify retryPromise using modern JS (#1086)

This commit is contained in:
Raphael von der Grün 2020-10-06 10:56:21 +02:00 committed by GitHub
parent 5d3591b853
commit e125ab1b9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,7 +21,7 @@
var events = require('cordova-common').events;
/*
/**
* Retry a promise-returning function a number of times, propagating its
* results on success or throwing its error on a failed final attempt.
*
@ -31,31 +31,13 @@ var events = require('cordova-common').events;
*
* @returns {Promise}
*/
module.exports.retryPromise = function (attemptsLeft, promiseFunction) {
// NOTE:
// get all trailing arguments, by skipping the first two (attemptsLeft and
// promiseFunction) because they shouldn't get passed to promiseFunction
var promiseFunctionArguments = Array.prototype.slice.call(arguments, 2);
return promiseFunction.apply(undefined, promiseFunctionArguments).then(
// on success pass results through
function onFulfilled (value) {
return value;
},
// on rejection either retry, or throw the error
function onRejected (error) {
attemptsLeft -= 1;
if (attemptsLeft < 1) {
throw error;
}
module.exports.retryPromise = async function (attemptsLeft, promiseFunction, ...args) {
while (true) {
try {
return await promiseFunction(...args);
} catch (error) {
if (--attemptsLeft < 1) throw error;
events.emit('verbose', 'A retried call failed. Retrying ' + attemptsLeft + ' more time(s).');
// retry call self again with the same arguments, except attemptsLeft is now lower
var fullArguments = [attemptsLeft, promiseFunction].concat(promiseFunctionArguments);
return module.exports.retryPromise.apply(undefined, fullArguments);
}
);
}
};