Skip to content

Instantly share code, notes, and snippets.

@softwarespot
Last active August 30, 2017 17:44
Show Gist options
  • Save softwarespot/5fe3c6360d4d4993d76cb5b54f9de540 to your computer and use it in GitHub Desktop.
Save softwarespot/5fe3c6360d4d4993d76cb5b54f9de540 to your computer and use it in GitHub Desktop.
/* eslint-disable */
let id = 0;
function retryAsync(fn) {
const guid = arguments[1] || {};
return new Promise(resolve => {
resolve(fn(() => guid));
})
.then(value => value === guid ?
retryAsync(fn, guid) :
value);
}
const onComplete = retryAsync(retry => {
id += 1;
return fetch()
.then(() => {
console.log('Id:', id);
return id < 4 ? retry() : id;
});
});
onComplete
.then(value => console.log('Completed', value))
.catch(err => console.error('Error', err));
function fetch() {
return new Promise(resolve => {
setTimeout(resolve, 100);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment