Skip to content

Instantly share code, notes, and snippets.

@Nautigsam
Created April 28, 2016 14:41
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 Nautigsam/f03e03f70754380bf940c4daa6362d1d to your computer and use it in GitHub Desktop.
Save Nautigsam/f03e03f70754380bf940c4daa6362d1d to your computer and use it in GitHub Desktop.
Show the use of loopCall, which call a function (here failedFunc) and return a promise immadiately or after maxCount tries in case of error.
var failedFunc = () => Promise.reject()
var maxCount = 3;
(function loopCall (maxCount, count) {
if (count == undefined) count = 0
return failedFunc()
.catch((err) => {
count++
if (count == maxCount) return Promise.reject(err)
return loopCall(maxCount, count)
})
})(maxCount)
.then(() => {
console.log(`Succeeded`)
}, (err) => {
console.log(`Failed`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment