2016-12-30 143 views
1

我尝试使用异步库的NodeJS执行收集API的信息,但async.parallel回调收集,我需要异步回调的NodeJS

这所有的数据之前执行自身后快递渲染是代码:

  LolApi.getMatchHistory(app_dtb.summoner.id, "euw", function(err, history) { 
      if (!err) { 
       async.parallel([ 
        function(callback) { 
         LolApi.getMatch(history.matches[nbMatch].matchId, function(err, match) { 
          if (!err) { 
           var teamIn; 

           function getParticipantNb() { 
            for (var i = 0; i < match.participantIdentities.length; i++) { 
             if (app_dtb.summoner.id == match.participantIdentities[i].player.summonerId) { 
              if (i <= 5) teamIn = 100 
              else teamIn = 200 
              return i + 1 
             } 
            } 
            return false; 
           } 

           var participantNb = getParticipantNb() 
           if (match.teams[0].winner == true && teamIn == 100) { 
            app_dtb.lastGame.won = "Win"; 
           } else { 
            app_dtb.lastGame.won = "Loose"; 
           } 
           console.log(app_dtb.lastGame.won) 
          } else { 
           console.log(err) 
          } 

         }); 
         setTimeout(function() { 
          callback(null, "one"); 
         }, 200); 
        }, 
        function(callback) { 
         options = { 
          champData: 'allytips,blurb', 
          version: '4.4.3', 
          locale: 'en_US' 
         } 
         LolApi.Static.getChampionById(history.matches[nbMatch].champion, options, function(err, champ) { 
          if (!err) { 
           console.log(champ.name); 
           app_dtb.lastGame.champName = champ.name 

          } else { 
           console.log(err) 
          } 

         }); 
         setTimeout(function() { 
          callback(null, "two"); 
         }, 100); 
        } 
       ], function(err, results) { 
        console.log(results) 
        res.render("index"); 
       }); 
      } else { 
       console.log(err); 
      } 

     }) 

任何想法或其他方式有相同的结果?

非常感谢

+1

你的代码有什么问题或者你正试图解决的问题? –

+0

我试图呈现索引后的一系列console.log记录我需要的一切,但目前索引呈现本身在日志结束之前 –

回答

0

你应该打电话给你LolApi回调方法里面的callback和酸酸的async回调最终将要求两个平行的功能。因此可能在LolApi回调之前呼叫timeout

+0

这似乎工作,而且我删除了超时(我不知道是否有效果)谢谢! –