I
I
ince2019-05-22 15:14:05
JavaScript
ince, 2019-05-22 15:14:05

How to write an asynchronous recursive function?

How can an asynchronous recursive call be implemented?
It is necessary after receiving a response from each ajax request, using the results of the previous one, send a new one, and so on until a certain condition is reached.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
zendor, 2019-05-22
@ince

For example use async/await:

async function foo(val = 0) {
  const response = await request(val); //предполагается, что request возвращает promise
  if (response.error) throw new Error('something bad happened');
  //делаем что-то с response
  foo(response.newVal); //вызываем foo с новым val
}

P
Peter, 2019-05-22
@petermzg

Promise

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question