Last active
August 29, 2015 12:09
-
-
Save CarlLee/9fb961270a4f50dd3eee to your computer and use it in GitHub Desktop.
async_conditioning with [async module](https://github.com/caolan/async)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var async = require('async'); | |
var bools = {}; | |
function check1(){ | |
// do something | |
bools['check1'] = condition1; | |
} | |
function check2(){ | |
// do other things | |
bools['check2'] = condition2; | |
} | |
async.parallel([check1, check2], function(err, results){ | |
var finalBool = true; | |
for(var attr in bools){ | |
finalBool &= bools[attr]; | |
} | |
if(finalBool){ | |
//all true | |
}else{ | |
//at least one false | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var async = require('async'); | |
function check1(callback){ | |
// do something | |
callback(null, condition1); | |
} | |
function check2(callback){ | |
// do other things | |
callback(null, condition2); | |
} | |
async.parallel([check1, check2], function(err, results){ | |
// results is always in the order of [condition1, condition2] no matter check1 or check2 finishes first | |
var finalBool = true; | |
results.forEach(function(result){ | |
finalBool &= result; | |
}); | |
if(finalBool){ | |
//all true | |
}else{ | |
//at least one false | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment