JavaScript Synchronous Loops

可紊 提交于 2019-12-23 13:09:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!