Reputation: 3
How to make this for cycle synchronous?
Code
// (...)
object = {
'item1': 'apple',
'item2': 'orange'
};
// (...)
for(var key in object) {
// do something async...
request.on('response', function (response) {
response.on('data', function (chunk) {
console.log('The message was sent.');
});
});
}
console.log('The for cycle ended.');
Output
The for cycle ended.
The message was sent.
I would like to see this type of output...
The message was sent.
The for cycle ended.
Upvotes: 0
Views: 1238
Reputation: 1075159
Updated answer:
Re your updated question, the call to sendMessage
is synchronous, so you must be calling a function that does something asynchronous (as I mentioned below). sendMessage
isn't listed in the NodeJS docs. You'll have to find a synchronous version of it from whatever source it was that you got it from, or use its callback mechanism:
var obj, keys, key, index;
// Define the object
obj = {
'item1': 'apple',
'item2': 'orange'
};
// Find its keys (you can just type in the array if they don't
// need to be discovered dynamically)
keys = [];
for (key in obj) {
keys.push(key);
}
// Start the loop
index = 0;
process();
// This function gets called on each loop
function process() {
// Are we done?
if (index >= keys.length) {
// Yes
console.log("The cycle ended");
}
else {
// No, send the next message and then
// use this function as the callback so
// we send the next (or flag that we're done)
sendMessage(obj[keys[index++]], process);
}
}
Original answer:
The cycle is synchronous. You'd have to do something like a setTimeout
or something to make it *a*synchronous.
The calls you're making to NodeJS may not be synchronous, though. You have to call the xyzSync
versions of things if you want synchronous calls.
Continuing to guess at what you might mean, if you wanted to make the loop *a*synchronous:
var obj, key;
// Define the object
obj = {
'item1': 'apple',
'item2': 'orange'
};
for (key in obj) {
schedule(key);
}
function schedule(k) {
setTimeout(function() {
// Do something with obj[k]
}, 0);
}
Upvotes: 5
Reputation: 41832
I am not familiar with node.js, but I would presume that:
function() {
console.log('The message was sent.');
}
is a callback function that is called once the message was successfully sent. And that the actual sending of the message is asynchronous, so as to not be blocking on the rest of the execution. If you want to make this a blocking/synchronous process, you can do something like this: (note, there may be an explicit way to make a synchronous call in node.js, I'm just not familiar with it):
for(var key in object) {
var completed = false;
sendMessage('Hello!', function() {
console.log('The message was sent.');
completed = true;
});
while(completed == false) {
; // do nothing
}
}
The downside of the above approach is you could find yourself in an infinite loop on the while() statement if there is ever an error in sendMessage() or the callback.
Another approach which would allow you to send all the messages asynchronously but then wait for them to all complete before moving on would be to do something such as:
var count = 0;
for(var key in object) {
count++;
sendMessage('Hello!', function() {
console.log('The message was sent.');
count--;
});
}
while(count > 0){
; // wait until all have finished
}
This would have the same problem of infinite loop if there was ever an error that prevent count from reaching 0 again.
Upvotes: 0