Skip to content

Instantly share code, notes, and snippets.

@akidee
Created December 9, 2010 07:11
Show Gist options
  • Save akidee/734436 to your computer and use it in GitHub Desktop.
Save akidee/734436 to your computer and use it in GitHub Desktop.
Instead of the serial end() - every item in the source list is processed after the previous one is ready - endParallel() makes parallel calls to a generator that uses filters that can be executed separately.
var async = require('asyncjs')
async.plugin({
endParallel: function(callback) {
var source = this.source
var e
var min_i_err = Infinity
var first_e = null
var values = []
var counter = 0
var handler = function(i) {
return function(err, value) {
e = err
if (err) {
if (i < min_i_err)
min_i_err = i
first_e = err
/*if (err == STOP)
callback && callback(null, last)
else
callback && callback(err, value)*/
}
else {
values[i] = value
}
process.nextTick(function() {
if (--counter == 0)
callback && callback(first_e == async.STOP ? null : first_e, values[values.length - 1])
})
}
}
do {
source.next(handler(counter++))
} while (!e)
}
})
var t
async.list([
function(next) {
t = +new Date
async
.list([1,2,3,4,5,6,7])
.filter(function(i, next) {
time = Math.round(Math.random() * 1000)
console.log(i, 'yet '+time)
setTimeout(function() {
console.log(i, 'now');
next(null, i < 5);
}, time)
})
.end(function(e, v) {
console.log('serial', arguments[1], +new Date - t)
next()
})
},
function(next) {
t = +new Date
async
.list([1,2,3,4,5,6,7])
.filter(function(i, next) {
time = Math.round(Math.random() * 1000)
console.log(i, 'yet '+time)
setTimeout(function() {
console.log(i, 'now');
next(null, i < 5);
}, time)
})
.endParallel(function(e, v) {
console.log('parallel', arguments[1], +new Date - t)
next()
})
}
]).call().end()
@akidee
Copy link
Author

akidee commented Dec 9, 2010

$ node async_parallel.js 
1 'yet 504'
1 'now'
2 'yet 840'
2 'now'
3 'yet 160'
3 'now'
4 'yet 470'
4 'now'
5 'yet 130'
5 'now'
6 'yet 928'
6 'now'
7 'yet 126'
7 'now'
serial 4 3188
1 'yet 85'
2 'yet 582'
3 'yet 828'
4 'yet 922'
5 'yet 315'
6 'yet 974'
7 'yet 486'
1 'now'
5 'now'
7 'now'
2 'now'
3 'now'
4 'now'
6 'now'
parallel 4 980

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment