1

I think I might not have the right idea of how async.js work. In the following example the callback function is not executed and I am not sure why.

async.parallel([
  function(){
    console.log('one');
  },function(){
    console.log('two');
  }
], function(err, results) {
  console.log('done!');
});

In the console, I have:

one
two

I made a pen for this code too http://codepen.io/tnguyen14/pen/GaJxl

I have read the documentation from the async website https://github.com/caolan/async#parallel several times and tried to search online for examples of how to make this work, but I still don't get it.

I would appreciate it if someone could explain to me how async works and what's wrong with my code.

1 Answer 1

0

Async expect asynchronous control flow from it's arguments, e.i function you pass signals about completion by calling supplied callback function.

async.parallel calls sequentially each function from input list and then result callback after n input functions signalled completion exactly n times.

async.parallel([
  function(callback){
    console.log('one');
    $('.thing').html('one');
    callback(); // this is the way to tell async.parallel "I'm done". It can happen from different stack frame
  },function(callback){
    console.log('two');
    $('.thing').html('one');
    setInterval(callback, 1000); // just to make it more async-ly.
  }
], function(err, results) {
  console.log('done!');
  $('.result').html('one');
} );

Fork of your example on codepen: http://codepen.io/anon/pen/KBhob

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.