JavaScript同步循环 [英] JavaScript Synchronous Loops
问题描述
我有一个,用于处理JavaScript异步行为
循环,我知道将在成千上万的范围内,将节点暂停一个循环,并回到它,或者我可以安全地假设节点将返回我的函数之前执行循环完成?
这是循环有问题:
function fixData(dataset,blockcount){
for(var i = 0,len = dataset.length; i var obj = dataset [i ]。
obj._id = obj.name;
obj.expires = blockcount + obj.expires_in;
删除obj.expires_in;
}
返回数据集;
循环在Node中是同步的。 JS和JavaScript和同步代码总是运行完成。所以,如果你不打电话给异步功能,你可以放心,直到它完成,你的代码不会被打断。
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;
}
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.
这篇关于JavaScript同步循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!