Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Parallel example does not work! #31

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

3 participants

vwilliam Caolan McMahon Ian Walter
vwilliam

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?

Deleted user

The content you are editing has changed. Reload the page and try again.

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?

Sending Request…

Attach images by dragging & dropping or selecting them. Octocat-spinner-32 Uploading your images… Unfortunately, we don't support that file type. Try again with a PNG, GIF, or JPG. Yowza, that's a big file. Try again with an image file smaller than 10MB. This browser doesn't support image attachments. We recommend updating to the latest Internet Explorer, Google Chrome, or Firefox. Something went really wrong, and we can't process that image. Try again.

Deleted user

The content you are editing has changed. Reload the page and try again.

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"}

Sending Request…

Attach images by dragging & dropping or selecting them. Octocat-spinner-32 Uploading your images… Unfortunately, we don't support that file type. Try again with a PNG, GIF, or JPG. Yowza, that's a big file. Try again with an image file smaller than 10MB. This browser doesn't support image attachments. We recommend updating to the latest Internet Explorer, Google Chrome, or Firefox. Something went really wrong, and we can't process that image. Try again.

Ian Walter

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

Caolan McMahon caolan closed this January 15, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.