0

**I want to get result from findepisodecount function first then send response but it send response before getting response from a function.is there is anything i am doing or i need to use something else so that code can work perfectly **

  async.parallel([
    /*
     * First external endpoint
     */
    function(callback) {
         Title.title.find({ "primaryTitle" : { $regex:  /Dallas$/ } }).limit(10).lean()
                        .exec(function(err, docs) {
                           // var document= docs.json();

                           var document=  docs;
                         for (var i = 0, len = document.length; i < len; i++) 
                            {
                              //  console.log(docs[i].tconst);
                              if(document[i].titleType!="tvSeries")
                                {
                                            // console.log(docs[i].titleType);
                                }
                              else
                                {
                                    console.log("tv series");
                                    var valueofi = i ;
                                    Title.findepisodecount(document[valueofi],function(fulldocument)
                                    {
                                             document[valueofi].season = fulldocument;
                                           console.log("cacA");

                                              //outercallback();

                                    });
                                //       console.log(i)
                                }
                            }  
                              console.log("helloo");
                              callback(false,  document);    

      });
    }
  ],
  /*
   * Collate results
   */
  function(err, results) {
    if(err) { console.log(err); res.send(500,"Server Error"); return; }
    console.log(results);
    res.send(results);
    //res.send({api1:results[0], api2:results[1]});
  }
  );
Malik Kamran Abid
  • 321
  • 1
  • 3
  • 14

1 Answers1

0

Unless you have more code, I think async.waterfall() would be a better use case for what you are doing. The reason being is that you first want to get the titles, then convert each title to include their season/episode count for each of them.

We also use async.mapSeries to convert each title to include their season.

async.waterfall([
    function findTitles(callback) {
        Title.title
            .find({ "primaryTitle" : { $regex:  /Dallas$/ }})
            .limit(10)
            .lean()
            .exec(callback);
    },
    function findSeason(titles, callback) {

        // you are iterating over the documents and doing an async operation to convert each title document, use #async.mapSeries() 

        async.mapSeries(titles, function (title, callback) {
            // note the scope of the argument callback: this inner callback is NOT the same as outer callback

            // do nothing and skip
            if (title.titleType != "tvSeries")
                return callback(null, title);

            // otherwise
            Title.findepisodecount(title, function (season) {
                title.season = season;
                callback(null, title);
            });

        // when the iteration is all done, call the outer callback
        }, callback);


    }
], function (err, titles) {
    console.log('err', err);
    if (err)
        return res.send(500, "Server Error");
    console.log('results', titles);
    res.send(titles);
});

Also, this is a good read on what happens when you mix synchronous for with asynchronous operations.

Mikey
  • 6,728
  • 4
  • 22
  • 45