Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite retry as promise based #8524

Merged
merged 2 commits into from
Jul 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions packages/amazon-cognito-identity-js/src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,16 @@ const isNonRetryableError = (obj) => {
return obj && obj[key];
};

async function retry(functionToRetry, args, delayFn, attempt = 1) {
function retry(functionToRetry, args, delayFn, attempt = 1) {
if (typeof functionToRetry !== 'function') {
throw Error('functionToRetry must be a function');
}

logger.debug(`${functionToRetry.name} attempt #${attempt} with args: ${JSON.stringify(args)}`);

try {
return await functionToRetry(...args);
}
catch (err) {
return functionToRetry(...args).catch((err) => {
logger.debug(`error on ${functionToRetry.name}`, err);

if (isNonRetryableError(err)) {
logger.debug(`${functionToRetry.name} non retryable error`, err);
throw err;
Expand All @@ -197,13 +194,12 @@ async function retry(functionToRetry, args, delayFn, attempt = 1) {
logger.debug(`${functionToRetry.name} retrying in ${retryIn} ms`);

if (retryIn !== false) {
await new Promise(res => setTimeout(res, retryIn));
return await retry(functionToRetry, args, delayFn, attempt + 1);
}
else {
return new Promise(res => setTimeout(res, retryIn))
.then(() => retry(functionToRetry, args, delayFn, attempt + 1))
} else {
throw err;
}
}
})
}

function jitteredBackoff(maxDelayMs) {
Expand Down