So if you’re a back-end node.js dev, you’ll know about the awesome lib called async .
If you’re a front-end dev, you’ll know about the awesome lib called underscore.
Now the thing is, both of these libs tend to provide similar features to some extent.
So the question is, does it make sense to use async on the front end using browserify ?
Underscoreis a utility library that provides some useful functions likeeach,mapandreduce. But, all of these work synchronously. For exampleOutput:
[2, 4, 6]
If you notice, the
console.log(results)statement gets called only after the_.map()function is fully executed and the results returned. This is the typical synchronous style of programming that you use in browser scripting.On the server, where
Node.jsis the king, synchronous programming as above is harmful to the event loop. There, asynchronous style of programming is preferred. Take a look at the samemapmethod using theasynclibrary.Output:
[2, 4, 6]
If you notice, it doesn’t return the mapped array as return value, instead the mapped array is passed to the callback function and the
console.log(results)is used to print the results inside of the callback.One side effect of this style of programming is that the
iteratorfunction gets called in parallel, not in serial order thereby enabling way more scalability if the iterator function uses any I/O.So, even though some of the functions offered by
asynclibrary is similar to the one’s offered byUnderscore, they are for different purposes as demonstrated above. Now, the decision of which ones to use depends on your application requriements and programming style.