I
I
Igor Bezlepkin2019-08-24 14:18:44
JavaScript
Igor Bezlepkin, 2019-08-24 14:18:44

How to recurse with a promise in Javascript?

You need to make the function recursive with a promise.

function a () {
  b().then(() => {})

  function b () {
    return new Promise((resolve, reject) => {
      for (let i = 0; i < 10; i++) {
        if (i < 10) {
          b()
        } else {
          resolve(true)
        }
      }
    }
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max, 2019-08-24
@mbelskiy

if (i < 10) {
    b().then(() => resolve(true))
} else {

But just keep in mind that the current implementation will go into "infinite" recursion

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question