开发者

asyn vs sync forEach,map,filter

Looking at Async and a async branch of underscore.js it appears asyn versions of the Sync forEach,map, and开发者_Go百科 filter are present.

Considering Javascript is single threaded what value is there in returning a sorted,mapped,forEached... array vs passing the returned value to a callback function?


Asynchronous iteration functions like this are useful when what you do with each item depends on an underlying asynchronous task.

For instance, in Node.js, filesystem operations are asynchronous (they happen in a separate, non-JavaScript thread and call a callback when they’re done). If you want to, say, map a list of filenames to information on those files, you won’t be able to return a result to map until the filesystem operation calls back. Similarly, JavaScript database drivers tend to be asynchronous (they do their work in another thread, and call back a JavaScript function when they’re done).

For example, let’s say you have a list of user IDs (like a list of users involved in a Stack Overflow question), and you’d like to map them to detailed information about each user. With async.js, that might look like this:

async.map([2389, 6344, 27],
    function(id, callback){
        database.users.find({ id: id }, function(error, user){
            if (error) {
                callback(error);
            } else {
                callback(null, {
                    name: user.name,
                    reputation: user.reputation,
                    picture: user.picture
                });
            }
        })
    },
    function(error, results){
        /* results contains:
            [
                { name: "Bob", reputation: 10, picture: "http://…" },
                { name: "Fred", reputation: 2910, picture: "http://…" },
                { name: "Steve", reputation: 25000, picture: "http://…" }
            ]
        */
    }
);

The iterator function will be called for each item in the array, and will start a database query to get details about that user. It won’t wait for each query to finish before starting the next one. The database queries might finish very quickly, or they might take a little while. If they were synchronous, they could only be run one at a time. Worse, the JavaScript thread wouldn’t be able to do anything else while the database queries were running.

This way, it doesn’t matter how long the database queries take. While they go, the JavaScript thread might be idle, or it might be off dealing with something else (like another request to the server).

When each database request finishes (and they may not even finish in the same order they were started!), it calls its callback function, which puts the information about the user into an object and hands it to its callback. async.js keeps track of which iterations have returned so far, and calls the final callback only once every one of them has finished.

You can find similar examples in the async.js documentation.

In the browser there are fewer asynchronous operations, but you might use async.js with requests to the server, to load a set of resources (like scripts or images) and make sure they all loaded successfully, or send messages between windows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜