Skip to content

Instantly share code, notes, and snippets.

@bayleedev
Created December 4, 2016 02:29
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 bayleedev/6da0ebeaf408853b428d851b1decd035 to your computer and use it in GitHub Desktop.
Save bayleedev/6da0ebeaf408853b428d851b1decd035 to your computer and use it in GitHub Desktop.
function testApiRecursive (data) {
console.log('calling it', data)
return Promise.resolve().then(() => {
if (Math.round(Math.random() * 10000) % 20 === 0) {
// true 1/20th of the time
return data
} else {
return new Promise((resolve) => {
setTimeout(resolve, 250) // wait 250ms to run again
}).then(testApiRecursive.bind(this, data))
}
})
}
testApiRecursive(255).then((data) => {
console.log('tadah', data)
}).catch((error) => {
console.log('error', error)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment