Hi fellow Javascript/Node.js-Developer,
I'm running into the good old problem with asynchronous Javascript giving me only the last item of an array (as seen HERE and HERE). Unfortunately neither of the provided solutions worked for me.
I'm running on Node version 0.10.25. I compiled a minimal (not) working example:
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);
}
Expected output:
[1] Connected to hana-database
Checking table: ipfix_exporters
Checking table: ipfix_messages
Table 'ipfix_exporters' does exist!
Table 'ipfix_messages' does exist!
Actuall output:
[1] Connected to hana-database
Checking table: ipfix_exporters
Checking table: ipfix_messages
Table 'ipfix_messages' does exist!
Table 'ipfix_messages' does exist!
I'm totally stumped. Hopefully someone