Last active
December 31, 2015 02:39
-
-
Save mikechambers/7922389 to your computer and use it in GitHub Desktop.
Couple of examples showing how to call multiple functions asynchronously in series using the async library. Examples also show how to pass arguments to the functions being called in series (look at the second function).
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 async = require("async"); | |
| var child_process = require("child_process"); | |
| var config = {}; | |
| config.PROCESS_TIMEOUT = 10000; | |
| //these need to return functions to be executed. | |
| var processExecuter = function(processName, args, callback) { | |
| var cmd = child_process.execFile( | |
| processName, | |
| args, | |
| {timeout: config.PROCESS_TIMEOUT}, | |
| function (err, stdout, stderr) { | |
| var out = stdout; | |
| if(err) { | |
| out = stderr; | |
| } | |
| //note, if not error, err will be null | |
| callback(err, out); | |
| } | |
| ) | |
| } | |
| var foo = "bar"; | |
| async.series([ | |
| function (callback){processExecuter("echo", ["one"], callback)}, | |
| function (callback){processExecuter("echo", ["two"], callback)}, | |
| function (callback){processExecuter("echo", ["three"], callback)} | |
| ], | |
| // optional callback | |
| function(err, out){ | |
| if(err) { | |
| console.log("Error", err, out); | |
| return; | |
| } | |
| console.log(out); | |
| }); |
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 async = require("async"); | |
| var child_process = require("child_process"); | |
| var generateChildProcessPassThroughCallback = function(callback) { | |
| var childProcessPassThroughCallback = function (err, stdout, stderr) { | |
| var out = stdout; | |
| if(err) { | |
| out = stderr; | |
| } | |
| //note, if not error, err will be null | |
| callback(err, out); | |
| }; | |
| return childProcessPassThroughCallback; | |
| } | |
| var config = {}; | |
| config.PROCESS_TIMEOUT = 10000; | |
| //these need to return functions to be executed. | |
| var one = function (callback) { | |
| var cmd = child_process.execFile( | |
| "echo", | |
| ["one"], | |
| {timeout: config.PROCESS_TIMEOUT}, | |
| generateChildProcessPassThroughCallback(callback) | |
| ); | |
| }; | |
| var two = function (address, callback) { | |
| var cmd = child_process.execFile( | |
| "echo", | |
| [address], | |
| {timeout: config.PROCESS_TIMEOUT}, | |
| generateChildProcessPassThroughCallback(callback) | |
| ); | |
| }; | |
| var three = function (callback) { | |
| var cmd = child_process.execFile( | |
| "echo", | |
| ["three"], | |
| {timeout: config.PROCESS_TIMEOUT}, | |
| generateChildProcessPassThroughCallback(callback) | |
| ); | |
| }; | |
| var foo = "bar"; | |
| async.series([ | |
| one, | |
| function (callback){two(foo, callback)}, | |
| three | |
| ], | |
| // optional callback | |
| function(err, out){ | |
| if(err) { | |
| console.log("Error", err, out); | |
| return; | |
| } | |
| console.log(out); | |
| }); |
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 async = require("async"); | |
| //this is nice because the main function can stand on its own | |
| var doSomething = function(d, callback) { | |
| callback(null, d.toUpperCase()); | |
| } | |
| var doAsyncCall = function() { | |
| var data = null; | |
| //these call call out to main function, passing in regular args | |
| var one = function (callback) { | |
| //this can be set in a callback before the main callback is made | |
| data = "foo"; | |
| callback(null, "one"); | |
| }; | |
| var two = function (callback) { | |
| doSomething(data, function(err, out){ | |
| console.log("two : " + out); | |
| callback(err, out); | |
| }); | |
| }; | |
| var three = function (callback) { | |
| callback(null, "three"); | |
| }; | |
| async.series([ | |
| function (callback){one(callback)}, | |
| function (callback){two(callback)}, | |
| function (callback){three(callback)} | |
| ], | |
| // optional callback | |
| function(err, out){ | |
| if(err) { | |
| console.log("Error", err, out); | |
| return; | |
| } | |
| console.log("final : " + data); | |
| console.log(out); | |
| }); | |
| } | |
| doAsyncCall(); |
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 async = require("async"); | |
| var one = function (callback) { | |
| callback(null, "one"); | |
| }; | |
| var two = function (data, callback) { | |
| console.log("two : " + data); | |
| callback(null); | |
| }; | |
| var three = function (callback) { | |
| callback(null); | |
| }; | |
| var doAsyncCall = function() { | |
| async.waterfall([ | |
| one, | |
| two, | |
| three | |
| ], | |
| // optional callback | |
| function(err, out){ | |
| if(err) { | |
| console.log("Error", err, out); | |
| return; | |
| } | |
| //prints undefined, since three() doesnt pass any args | |
| //to the callback | |
| console.log(out); | |
| }); | |
| } | |
| doAsyncCall(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment