bonchef bonchef - 3 months ago 37
Javascript Question

Is Node.js Array.map() asynchronous?

Can I count on nodeIDs mapping is completed every time doSomething() is called?

nodeIDs = $.map(nodeIDs, function(n){
return n.match(/\d+$/);
});
doSomething(nodeIDs);


I thought all callbacks in node.js are asynchronous? I did read an article on general programming that callback could be synchronous but I am not sure about node.js?

Answer

JavaScript is also a functional programming language. What you have here is a «higher order function», a function which takes a function as a parameter. Higher order functions are synchronous.

Sources:

map() is a typical example of a higher order function. It takes a function and applies it to all elements of an array. The definition sounds very «functional». This function is also not provided by Node. It is documented by MDN Array.prototype.map() and specified by ECMAScript 5.1.

To answer your question: Yes, doSomething(nodeIDs) is called after all elements have been applied.

Comments