简体   繁体   中英

Wait for all callback to complete before returning objects

I have a function that needs to scrape a website and returns a list of addresses. In the callback from scrape, for each address returned I need to do another scraping operation then process the data, then I want to return the entire processed collection. I don't mind blocking if I have to. Ultimately I have to end up with a JSON object with the entire collection. Is this possible and how can I do this?

function doSomething(req, res){

    var collection = [];

    scrape1(params, function(error, addresses){
        if(!error){
            for(var i in addresses){   
                //do some stuff with addresses here

                scrape2(otherparams, function(error, address, data){
                    //manipulate the data here

                    collection.push({ 'address' : address, 'data' : data})  
                });
            }
            //this just returns an empty set obviously
            res.json(collection);

            //how can I return the entire collection from this function?
        }
    }); 
}

Here's one solution using the async module:

function doSomething(req, res){

  var collection = [];

  scrape1(params, function(error, addresses){
    if (error)
      return console.error(err); // handle error better

    async.each(addresses, function(address, callback) {
      scrape2(otherparams, function(err, address, data){
        // manipulate the data here
        if (err)
          return callback(err);
        collection.push({ 'address' : address, 'data' : data});
        callback();
      });
    }, function(err) {
      if (err)
        return console.error(err);  // handle error better
      res.json(collection);
    });
  }); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM