问题
I've been hitting my head on JavaScript's Async behavior when scaling functions from my test data to production work.
I have a for
loop that I know will range in the hundreds of thousands, will Node pause a for
loop and come back to it or can I safely assume that Node will execute the loop to completion before returning my function?
Here is the loop in question:
function fixData(dataset, blockcount){
for (var i = 0, len = dataset.length; i < len; i++) {
var obj = dataset[i];
obj._id = obj.name;
obj.expires = blockcount + obj.expires_in;
delete obj.expires_in;
}
return dataset;
}
回答1:
Loops are synchronous in Node.js and JavaScript and synchronous code always runs to completion. So if you're not making calls to asynchronous functions you can rest assured that your code will not be interrupted until it finishes.
来源:https://stackoverflow.com/questions/23717098/javascript-synchronous-loops