我在异步 javascript 方面不是很有经验。如何在不硬编码命令数量或使用 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);
});
});