A
A
Alexander Knyazev2017-02-09 10:43:59
JavaScript
Alexander Knyazev, 2017-02-09 10:43:59

How to call a function that returns a Promise inside that Promise's handler?

According to my logic (apparently incorrect), this code should be analogous to an infinite loop that outputs 22, 23, 24, and so on. But it only outputs 22 once. What is the reason?

function find() {
  let numberPage = 22;
  getPromiseOpenPage(numberPage);
  
  function getPromiseOpenPage (numberPage) {
    return new Promise( function (resolve, reject) {
      resolve(numberPage);
    })
  }
  getPromiseOpenPage().then(
    function (res) {
      console.log(res);
      getPromiseOpenPage(res++);
    }
  )
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Melnikov, 2017-02-09
@alexandrknyazev13071995

function find(start) {

  function iteration(number) {
    new Promise(function(resolve, reject) {
      resolve(number++)
    }).then(function() {
      console.log(number)
      if (number < 100) iteration(number)
    })
  }

  iteration(start)
}

find(22)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question