【发布时间】:2014-02-21 00:50:37
【问题描述】:
您好,Javascript/Node.js 开发人员,
我遇到了异步 Javascript 的老问题,只给我数组的最后一项(如 HERE 和 HERE 所示)。不幸的是,提供的解决方案都不适合我。
我在 Node 版本 0.10.25 上运行。我编译了一个最小(非)工作示例:
var neededTables = [{
name: "ipfix_exporters",
},{
name: "ipfix_messages",
}];
var params = {};
console.log('[1] Connected to hana-database');
neededTables.forEach(function(table) {
params.table = table;
console.log("Checking table: " + params.table.name);
checkForTable.bind(null, params)();
});
function checkForTable(thoseParams) {
setTimeout(
(function(myParams) { return function(err, rows) {
if(err) {
console.log(err);
return;
}
console.log("Table '"+myParams.table.name+"' does exist!");
}})(thoseParams), 1000);
}
预期输出:
[1] Connected to hana-database
Checking table: ipfix_exporters
Checking table: ipfix_messages
Table 'ipfix_exporters' does exist!
Table 'ipfix_messages' does exist!
实际输出:
[1] Connected to hana-database
Checking table: ipfix_exporters
Checking table: ipfix_messages
Table 'ipfix_messages' does exist!
Table 'ipfix_messages' does exist!
我完全被难住了。希望有人
【问题讨论】:
标签: javascript arrays node.js asynchronous