I’m not so experienced in async javascript. How can I call the commands without hardcoding the amount of commands or using eval?
var commands = [
// command 1: result: 0, stdout: ""
function (stdin, callback) {
callback(0, "");
},
// command 2: result: 1, stdout: ""
function (stdin, callback) {
callback(1, "");
},
// command 3: result: 0, stdout: ""
function (stdin, callback) {
callback(0, "");
},
// ...
];
var stdin = "foo";
var end = function (result, stdout) {
console.log(result);
console.log(stdout);
};
commands[0](stdin, function (result, stdout) {
commands[1](stdout, function (result, stdout) {
commands[2](stdout, end);
});
});
Final answer:
I’m using recursion to go through the commands array.
You pass the loop function an array of commands and the last callback to be called (you can also pass as a fourth optional parameter the index of the array in which you wish to start the loop – defaults to zero).
Code Example