Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Front-end: underscore.js or async.js via browserify?

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 ?

like image 680
tUrG0n Avatar asked Nov 07 '12 21:11

tUrG0n


People also ask

Is Underscore each async?

_. each is synchronous, and will only return after the function was executed on all items. If that. get is asynchronous, each won't help you with that.

Why is JavaScript Underscore better?

Instead of having to cater to the expectations of the JavaScript language, Underscore helps you write code that clearly expresses your intentions. The next developer - or the future you - will have an easier time understanding how you wanted your code to work.

What is Underscore js used for?

Underscore. js is a utility library that is widely used to deal with arrays, collections and objects in JavaScript. It can be used in both frontend and backend based JavaScript applications. Usages of this library include filtering from array, mapping objects, extending objects, operating with functions and more.


2 Answers

Underscore is a utility library that provides some useful functions like each, map and reduce. But, all of these work synchronously. For example

var results = _.map([1,2,3], function(value, index, list) {
  return value * 2;
});

console.log(results);

Output: [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.js is the king, synchronous programming as above is harmful to the event loop. There, asynchronous style of programming is preferred. Take a look at the same map method using the async library.

async.map([1,2,3],
        function mapper(item, callback) {
            callback(null, item * 2);
        },
        function(error, results) {
            console.log(results);
        }
);

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 iterator function 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 async library is similar to the one's offered by Underscore, they are for different purposes as demonstrated above. Now, the decision of which ones to use depends on your application requriements and programming style.

like image 141
Ganeshji Marwaha Avatar answered Oct 05 '22 04:10

Ganeshji Marwaha


According to the async readme it can be used directly in the browser. Using browersify seems excessive.

like image 40
thedjpetersen Avatar answered Oct 05 '22 04:10

thedjpetersen