Node.js: Best way to perform multiple async operations, then do something else?

后端 未结 3 1797
再見小時候
再見小時候 2020-12-05 16:18

In the following code I am trying to make multiple (around 10) HTTP requests and RSS parses in one go.

I am using the standard forEach construct on an a

3条回答
  •  心在旅途
    2020-12-05 17:17

    HACK-FREE SOLUTION

    Promises to be included in next JavaScript version

    The popular Promise libraries give you an .all() method for this exact use case (waiting for a bunch of async calls to complete, then doing something else). It's the perfect match for your scenario

    Bluebird also has .map(), which can take an array of values and use it to start a Promise chain.

    Here is an example using Bluebird .map():

    var Promise = require('bluebird');
    var request = Promise.promisifyAll(require('request'));
    
    function processAllFeeds(feedsToFetch) {    
        return Promise.map(feedsToFetch, function(feed){ 
            // I renamed your 'feed' fn to 'processFeed'
            return processFeed(feed) 
        })
        .then(function(articles){
            // 'articles' is now an array w/ results of all 'processFeed' calls
            // do something with all the results...
        })
        .catch(function(e){
            // feed server was down, etc
        })
    }
    
    function processFeed(feed) { 
        // use the promisified version of 'get'
        return request.getAsync(feed.url)... 
    }
    

    Notice also that you don't need to use closure here to accumulate the results.

    The Bluebird API Docs are really well written too, with lots of examples, so it makes it easier to pick up.

    Once I learned Promise pattern, it made life so much easier. I can't recommend it enough.

    Also, here is a great article about different approaches to dealing with async functions using promises, the async module, and others

    Hope this helps!

提交回复
热议问题