乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      javascript – 節(jié)點(diǎn)js for-loop在下一次迭代之前等待異步函數(shù)?

       印度阿三17 2019-10-02

      我正在創(chuàng)建一個(gè)每隔一段時(shí)間刷新一次數(shù)據(jù)的函數(shù),而且我遇到了我的請求鏈問題.問題是我有一個(gè)for循環(huán)運(yùn)行異步請求,for循環(huán)將在請求完成之前完成.

      setInterval(function(){ // this updates the total hours of all members every 10 seconds
          request({ // this gets all of the loyalty program members
              url: "",//omitted
              method: "GET"
          },
              function(listError, listResponse, listBody) {
                  if(listError == null && listResponse.statusCode == 200) {
                      var varBody = {};
                      var listObj = JSON.parse(listBody);
                      for(var i = 0; i < listObj.result.length; i  ) { // parses through all of the members to update their hours
      
                          console.log(i);//****PRINT STATEMENT
      
                          varBody.index = i;
                          varBody.memberID = listObj.result[i].program_member.id;
                          request({ //we do this request to get the steam ID of the program member
                                  url: "",//omitted
                                  method: "GET"
                              },
                              function(fanError, fanResponse, fanBody) {
      
                                  var fan = JSON.parse(fanBody);
                                  if(fanError == null && fanResponse.statusCode == 200 && fan.result.profiles.length != 0) { // make sure that the profile isn't empty
                                      request({
                                              url:"",//omitted
                                              method: "GET"
                                          },
                                          function(hourError, hourResponse, hourBody) {
                                              if (hourError == null && hourResponse.statusCode == 200) {
                                                  var gameList = JSON.parse(hourBody);
                                                  var minutes = 0;
                                                  for (var j = 0; j < gameList.response.games.length; j  ) { // for loop to calculate the minutes each user has on steam
                                                      minutes  = gameList.response.games[j].playtime_forever;
                                                  }
                                                  var deltaHours = 1;
                                                  if(deltaHours != 0) {
                                                      var transaction = { // updated member object to be inserted
                                                          pointsearned: deltaHours,
                                                          pointsused: 0,
                                                          loyaltyprogram_id: loyaltyID,
                                                          programmember_id: memberID
                                                      };
                                                      request({ // POST request to update the member
                                                              url: "",//omitted
                                                              method: "POST",
                                                              body: JSON.stringify(transaction),
                                                              headers: {
                                                                  "Content-Type": "application/json"
                                                              }
                                                          },
                                                          function(updateError, updateRes, updateBody) {
                                                              if(updateError == null && updateRes.statusCode == 200) {
                                                                  console.log("Success");//****PRINT STATEMENT
                                                              }
                                                          }
                                                      );
                                                  }
                                              }
                                          }
                                      );
                                  }
                              }
                          );
                      }
                  }
                  console.log("Users Updated"); //****PRINT STATEMENT
              }
          );
      }, 10000);
      

      如果我要運(yùn)行此代碼,它將打印:

      0
      1
      2
      3
      Success
      Success
      Success
      Success
      

      我知道問題是什么.事實(shí)是for循環(huán)不等待請求完成.我不知道的是解決這個(gè)問題.有沒有人有任何想法?

      解決方法:

      你想要async庫.

      例如,

      for(var i = 0; i < listObj.result.length; i  ) {
          varBody.index = i;
          varBody.memberID = listObj.result[i].program_member.id;
          request(
              ...
          , function () {
              // Do more Stuff
          });
      }
      

      可以這樣寫:

      async.forEachOf(listObj.result, function (result, i, callback) {
          varBody.index = i;
          varBody.memberID = result.program_member.id;
          request(
              ...
          , function () {
              // Do more Stuff
              // The next iteration WON'T START until callback is called
              callback();
          });
      }, function () {
          // We're done looping in this function!
      });
      

      在異步中有很多方便的實(shí)用程序函數(shù),這使得回調(diào)更容易.

      來源:https://www./content-1-480351.html

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多