Last active
August 30, 2017 17:44
-
-
Save softwarespot/5fe3c6360d4d4993d76cb5b54f9de540 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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