Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallel example does not work! #31

Closed
ghost opened this issue May 16, 2011 · 3 comments
Closed

Parallel example does not work! #31

ghost opened this issue May 16, 2011 · 3 comments

Comments

@ghost
Copy link

ghost commented May 16, 2011

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
Copy link

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
Copy link

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
Copy link

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

@caolan caolan closed this as completed Jan 15, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants