Last Updated: July 15, 2016
·
2.226K
· levity

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

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#javascript
#js
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside