Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
v0.6.5 added wait.parallel extensions
  • Loading branch information
luciotato committed Aug 17, 2014
1 parent 762caa8 commit 85c75d7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
40 changes: 36 additions & 4 deletions README.md
Expand Up @@ -208,10 +208,10 @@ function my_seq_function(arg,arg...){
}
```
Extensions
Parallel Extensions
----------
wait.parallel.launch = function(functions)
wait.parallel.launch(functions:Array)
----------------------
Note: must be in a Fiber
Expand All @@ -234,7 +234,7 @@ do not "returns" until all fibers complete
throws if error
wait.parallel.map = function(arr,mappedFn)
wait.parallel.map(arr:Array, mappedFn:function)
----------------------
Note: must be in a Fiber
Expand All @@ -257,7 +257,7 @@ do not "returns" until all fibers complete
throws if error
wait.parallel.filter = function(arr, itemTestFn)
wait.parallel.filter(arr:Array, itemTestFn:function returns boolean)
----------------------
Note: must be in a Fiber
Expand All @@ -277,3 +277,35 @@ array with items where itemTestFn() returned true
do not "returns" until all fibers complete
throws if error
-------------
Parallel Usage Example:
see:
- (/parallel-tests.js)
##Notes on usage on non-standard callbacks. e.g.: connection.query from mysql
wait.for expects standardized callbacks.
A standardized callback always returns (err,data) in that order.
A solution for the sql.query method and other non-standard callbacks
is to create a wrapper function standardizing the callback, e.g.:
connection.prototype.q = function(sql, params, stdCallback){
this.query(sql,params, function(err,rows,columns){
return stdCallback(err,{rows:rows,columns:columns});
});
}
usage:
try {
var result = wait.forMethod(connection, "q", options.sql, options.params);
console.log(result.rows);
console.log(result.columns);
}
catch(err) {
console.log(err);
}
11 changes: 7 additions & 4 deletions parallel-tests.js
Expand Up @@ -12,7 +12,7 @@ wait.miliseconds = function(ms){
}

// -------------------
// RUN TESTS (Fiber)--
// RUN TESTS (in a Fiber, called from tests.js)
// -------------------
exports.runTests=function(hostname){

Expand All @@ -23,6 +23,9 @@ exports.runTests=function(hostname){
var addresses = wait.for(dns.resolve4,hostname);
console.log("addresses: ",JSON.stringify(addresses));

//----------------
//parallel map
//----------------
console.log('wait.parallel.map(addresses, dns.reverse)');
// get reverse addrs in parallel
// launch a task for each item, calling fn(item,inx,arr)
Expand All @@ -35,7 +38,9 @@ exports.runTests=function(hostname){
console.log(i,reverseAddrs[i]);
};

//parallel filter.
//----------------
//parallel filter
//----------------
console.log('wait.parallel.filter(addresses, likeIt(reverse)');
// launch a fiber for each item, calling fn(item,inx,arr)
var reverseLiked = wait.parallel.filter(addresses, function(item,inx){
Expand Down Expand Up @@ -66,5 +71,3 @@ exports.runTests=function(hostname){
}
};

// MAIN
//wait.launchFiber(exports.runTests,"google.com");

0 comments on commit 85c75d7

Please sign in to comment.