V
V
vasromand2015-02-01 14:39:27
Node.js
vasromand, 2015-02-01 14:39:27

How to write asynchronous code in conditional statements?

Follow-up to this question ( How to write asynchronous code correctly? ), but without a direct link.
There is a function in which data can be repeatedly modified asynchronously, after which it is passed to callback functions. When should a callback be made?

function(data, callback){
  if(data.someCondition1){
    doAsyncCode(data, function(err, result){
      ...
    }
  }
  if(data.someCondition2){
    doAsyncCode(data, function(err, result){
      ...
    }
  }
  // В конце нужен коллбэк
}

And how to ensure that the data is modified sequentially? Split into subfunctions, such as in the example by reference?
Where can one read about such examples?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri Shikanov, 2015-02-01
@dizballanze

The easiest way is to use the async module and its waterfall method .

D
Denis Pushkarev, 2015-02-01
@rock

The easiest way is not to suffer and use async / await from ES7, though so far only with a preprocessor, example , your specific case .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question