最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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 ?

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 ?

Share Improve this question edited Oct 13, 2015 at 5:03 user3335966 2,7454 gold badges31 silver badges33 bronze badges asked Nov 7, 2012 at 21:26 tUrG0ntUrG0n 4,4483 gold badges22 silver badges27 bronze badges 2
  • You should also check out nimble (caolan.github./nimble) it bines the best of both into one tiny file. – generalhenry Commented Nov 8, 2012 at 1:22
  • nimble does not seem pretty much maintained! – tUrG0n Commented Nov 8, 2012 at 11:13
Add a ment  | 

2 Answers 2

Reset to default 16

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.

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

发布评论

评论列表(0)

  1. 暂无评论