Loop with promises
const testPromise = function (value) {
return new Promise(function (res, rej) {
console.log('testPromise: ' + value)
value >= 3 ? res() : rej()
})
}
var value = 1
const poll = function () {
testPromise(value)
.then(() => {
console.log('yay')
})
.catch(() => {
setTimeout(() => {
value += 1
poll()
}, value * 1000)
})
}
poll()
See it in action here: https://jsbin.com/wirumixojo/edit?html,js,console
Written by Lawrence Wang
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#