Skip to content

Instantly share code, notes, and snippets.

@alfonsodev
Forked from Calvin-Huang/redux-saga-retry.js
Created December 3, 2017 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfonsodev/aef8c7a49bd8929f3853db68822ca165 to your computer and use it in GitHub Desktop.
Save alfonsodev/aef8c7a49bd8929f3853db68822ca165 to your computer and use it in GitHub Desktop.
// Retry 5 times with waiting for 2 seconds
function* updateApi(data) {
for(let i = 0; i < 5; i++) {
try {
const apiResponse = yield call(apiRequest, { data });
return apiResponse;
} catch(err) {
if(i < 4) {
yield call(delay, 2000);
}
}
}
// attempts failed after 5 attempts
throw new Error('API request failed');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment