P
P
Pavel Tkachenko2018-05-17 10:19:21
Node.js
Pavel Tkachenko, 2018-05-17 10:19:21

Loops in nodejs, how to implement correctly?

How to properly implement cycles in nodejs, when is it better to use promise, callback, closures, and async?
And is this form of executing sequential functions through promises correct?
Example

getData1(data)
    .then(getData2(data)
        .then(getData3(data)
            .then(...)
        )
    )

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vit, 2018-05-17
@fornit1917

The question is not entirely clear.
If you are making an asynchronous call, you are using a promise. If not, don't use it.
Your example with sequential operations is also incorrectly written. It would be more correct like this:

getData1()
  .then(data => {
    return getData2(data);
  })
  .then(data => {
    return getData3(data)
  })

V
Valery, 2018-05-17
@it_monk

let data = await getData1()
data = await getData2(data)
data = await getData3(data)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question