Almost all places in the node where callback functions are used are asynchronous. The code behind the callback functions is likely to be executed first than the code in the callback functions, especially database operations. Of course, node also provides functions for synchronous versions, such as file operations. fs.readFileSync() is the synchronous version of fs.readFile().
So the question is, is forEach() asynchronous? Logically speaking, if Sync is not added, it should be asynchronous.
The code copy is as follows:
var arr = ['a', 'b', 'c'];
var str = '123';
arr.forEach(function(item) {
str += item;
while (true) {}; //Use a dead loop to stuck it ~~
});
console.log(str);
Run the above code and it ends up being stuck, without any output. .
So, forEach() in node is synchronized! !
When I first used node, I didn't consider this issue, so I wrote it synchronously. I suddenly thought that after the test, I was shocked and thought that the previous code had been written incorrectly.
If in some cases, forEach needs to be processed asynchronously, Google it and there is a node-array, you can try it~~ Portal: https://github.com/cfsghost/node-array
This work was created by http://www.cnblogs.com/ImYZF