Parallel example does not work! #31

Closed
williamvivier opened this Issue May 16, 2011 · 3 comments

Projects

None yet

3 participants

@williamvivier

I tried the parallel example:
async.parallel([
function(callback){
setTimeout(function(){
callback(null, 'one');
}, 200);
},
function(callback){
setTimeout(function(){
callback(null, 'two');
}, 100);
},
],
// optional callback
function(err, results){
// in this case, the results array will equal ['two','one']
// because the functions were run in parallel and the second
// function had a shorter timeout before calling the callback.
});

But it returns ['one', 'two'] instead of ['two', 'one']

Any ideas why?

@ghost
ghost commented Aug 14, 2011

I'm trying to use this for async validations with an object of validations, like {email:.notNull().isEmail(), username: .len(5,20)}.

using async.parallel, it runs all the validations, but not asynchronously. no matter how long the first function (wrapping a chain of validations on one attribute) takes, it will always finish before the next function begins. do we have to call nextTick ourselves?

@ghost
ghost commented Aug 14, 2011

I guess .parallel does not delegate to .nextTick(). Using a setTimeout within the tasks, they do run asynchronously, but otherwise they block. Should we call process.nextTick() ourselves then within the individual tasks?

`
taskDone = (num, cb)->
return ()->
console.log "Done with: " + num
cb(null, "This is " + num)

tTasks = 
  zero: (cb) ->
    # ONLY THIS BLOCKS
    console.log "Starting ZERO"
    now = new Date().getTime()
    while(new Date().getTime() < now + 4000)
      a = 1
    taskDone("zero", cb)()
  one: (cb)-> 
    console.log "Starting ONE"
    setTimeout(taskDone("one", cb), 1000)
  two: (cb)-> 
    console.log "Starting TWO"
    setTimeout(taskDone("two", cb), 0)

tChecked = (err, results)-> console.log "Done: " + JSON.stringify(results)

async.parallel(tTasks, tChecked)   

`

Returns:

Starting ZERO
Done with: zero
Starting ONE
Starting TWO

Done with: two
Done with: one
Done: {"zero":"This is zero","two":"This is two","one":"This is one"}

@ianwalter

The docs have been updated explaining that this is the intended functionality.
Someone should close this issue.

@caolan caolan closed this Jan 15, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment