Last active
August 29, 2015 14:24
-
-
Save theRemix/bed7ff80bdec78382dcd to your computer and use it in GitHub Desktop.
Multiple Callbacks without any Promises
This file contains hidden or 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 asyncsDone = [false,false]; | |
| function fun1 (arg1, callback) { | |
| // do some async function using arg1 | |
| setTimeout(function () { | |
| arg1 += 10; | |
| // async function done | |
| callback(null, arg1); | |
| }, 1000); | |
| } | |
| function fun1done (err, result) { | |
| console.log('fun1done', result); | |
| asyncsDone[0] = true; | |
| asyncFinished(); | |
| } | |
| function fun2 (arg1, arg2, callback) { | |
| // do some async function using arg1 | |
| setTimeout(function () { | |
| arg1 += arg2 + 20; | |
| // async function done | |
| callback(null, arg1); | |
| }, 1200); | |
| } | |
| function fun2done (err, result) { | |
| console.log('fun2done', result); | |
| asyncsDone[1] = true; | |
| asyncFinished(); | |
| } | |
| function asyncFinished () { | |
| if( checkAllDone() ){ | |
| console.log('all done'); | |
| } | |
| } | |
| function checkAllDone () { | |
| return asyncsDone.reduce(function (p,c) { | |
| return p && c; | |
| }, true); | |
| } | |
| fun1(1, fun1done); | |
| fun2(2, 3, fun2done); |
This file contains hidden or 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
| /* | |
| functions used as arguments to this function must have the function signature `err, result` | |
| */ | |
| function manuallyHandlingTwoAsyncFunctions (fun1, fun1Args, fun2, fun2Args, done) { | |
| var asyncsDone = [false,false]; | |
| var allErrs = []; | |
| var allResults = []; | |
| var originalCallbacks = [fun1Args.pop(), fun2Args.pop()]; | |
| function checkAllDone () { | |
| return asyncsDone.reduce(function (p,c) { | |
| return p && c; | |
| }, true); | |
| } | |
| function finishedOne () { | |
| if( checkAllDone() ){ | |
| done(null, allResults); | |
| } | |
| } | |
| function new1Callback (err, result) { | |
| if(err) return done(err, allResults); // break out, never finish | |
| allResults[0] = result; | |
| originalCallbacks[0](err, result); | |
| asyncsDone[0] = true; | |
| finishedOne(); | |
| } | |
| function new2Callback (err, result) { | |
| if(err) return done(err, allResults); // break out, never finish | |
| allResults[1] = result; | |
| originalCallbacks[1](err, result); | |
| asyncsDone[1] = true; | |
| finishedOne(); | |
| } | |
| fun1Args.push(new1Callback); | |
| fun2Args.push(new2Callback); | |
| fun1.apply(this, fun1Args); | |
| fun2.apply(this, fun2Args); | |
| } | |
| function fun1 (arg1, callback) { | |
| // do some async function using arg1 | |
| setTimeout(function () { | |
| arg1 += 10; | |
| // async function done | |
| callback(null, arg1); | |
| }, 1000); | |
| } | |
| function fun1done (err, result) { | |
| console.log('fun1done', result); | |
| } | |
| function fun2 (arg1, arg2, callback) { | |
| // do some async function using arg1 | |
| setTimeout(function () { | |
| arg1 += arg2 + 20; | |
| // async function done | |
| callback(null, arg1); | |
| }, 1200); | |
| } | |
| function fun2done (err, result) { | |
| console.log('fun2done', result); | |
| } | |
| manuallyHandlingTwoAsyncFunctions(fun1, [1, fun1done], fun2, [2,3,fun2done], function (err, results) { | |
| if(err) throw err; | |
| var resultsOfFun1 = results[0]; | |
| var resultsOfFun2 = results[1]; | |
| console.log('resultsOfFun1',resultsOfFun1); | |
| console.log('resultsOfFun2',resultsOfFun2); | |
| }) |
This file contains hidden or 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
| /** | |
| * functions used as arguments to this function must have the function signature `err, result` | |
| * @param {Array<Function>} funs | |
| * @param {Array<Array<Arguments>>} funArgs | |
| * @param {Error<Null>->Array<Dynamic>->Void} done | |
| * | |
| * @return {Void} | |
| */ | |
| function dyamicallyHandleAnyAsyncFunctions (funs, funArgs, done) { | |
| var asyncsDone = funs.map(function(){return false}); | |
| var allErrs = []; | |
| var allResults = []; | |
| var originalCallbacks = funArgs.map(function(arrayOfArgs){return arrayOfArgs.pop();}); | |
| function checkAllDone () { | |
| return asyncsDone.reduce(function (p,c) { | |
| return p && c; | |
| }, true); | |
| } | |
| function finishedOne () { | |
| if( checkAllDone() ){ | |
| done(null, allResults); | |
| } | |
| } | |
| function createNewCallback(pos){ | |
| return function(err, result){ | |
| if(err) return done(err, allResults); // break out, never finish | |
| allResults[pos] = result; | |
| originalCallbacks[pos](err, result); | |
| asyncsDone[pos] = true; | |
| finishedOne(); | |
| } | |
| } | |
| funArgs.forEach(function (arrayOfArgs,i) { | |
| arrayOfArgs.push( createNewCallback(i) ); | |
| funs[i].apply(this, arrayOfArgs); | |
| }); | |
| } | |
| function fun1 (arg1, callback) { | |
| // do some async function using arg1 | |
| setTimeout(function () { | |
| arg1 += 10; | |
| // async function done | |
| callback(null, arg1); | |
| }, 1000); | |
| } | |
| function fun1done (err, result) { | |
| console.log('fun1done', result); | |
| } | |
| function fun2 (arg1, arg2, callback) { | |
| // do some async function using arg1 | |
| setTimeout(function () { | |
| arg1 += arg2 + 20; | |
| // async function done | |
| callback(null, arg1); | |
| }, 1200); | |
| } | |
| function fun2done (err, result) { | |
| console.log('fun2done', result); | |
| } | |
| function fun3 (arg1, arg2, callback) { | |
| // do some async function using arg1 | |
| setTimeout(function () { | |
| // async function done | |
| callback(null, arg1 * arg2); | |
| }, 1500); | |
| } | |
| function fun3done (err, result) { | |
| console.log('fun3done', result); | |
| } | |
| dyamicallyHandleAnyAsyncFunctions([fun1, fun2, fun3],[[1, fun1done], [2,3,fun2done], [2,3,fun3done]], function (err, results) { | |
| if(err) throw err; | |
| results.forEach(function (result, i) { | |
| console.log('async function #',i,'result:',result); | |
| }); | |
| console.log('all done!'); | |
| }) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: this does not handle errors very well, it just bails on the first error, and returns whatever results have been resolved up to that point.