Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
38
rated 0 times [  39] [ 1]  / answers: 1 / hits: 36045  / 12 Years ago, sat, october 6, 2012, 12:00:00

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?


More From » node.js

 Answers
82

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 (but see note below).



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.





Note: The higher order function is a concept of functional programming. JavaScript is functional, but also deeply seated in the practicality of executing code inside a browser or on the server. I would say that for example setTimeout() is not a higher order function even if it takes a function as a parameter because setTimeout() is not really purely functional because it uses time. Pure functionality is timeless. For example the result of map() doesn't depend on time. And that's what this question is really about. If something doesn't depend on time you execute it synchronously. Problem solved.



Thanks to Simon for challenging the definition of the higher order function in JavaScript.


[#82721] Thursday, October 4, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mariamiyanab

Total Points: 75
Total Questions: 102
Total Answers: 92

Location: British Indian Ocean Territory
Member since Tue, Feb 22, 2022
2 Years ago
;