Last Updated: July 15, 2016
·
1.995K
· 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