代码之家  ›  专栏  ›  技术社区  ›  Sadaf Chowdhury

如何设置变量异步瀑布?

  •  0
  • Sadaf Chowdhury  · 技术社区  · 8 年前

    jsonFinal 它进入下面的函数“数据”下。

    function getIndividualMatchJSONObjHelper(matchData, matchParticipantData, indexIter) {
    var individualMatchURL = 'https://na1.api.riotgames.com/lol/match/v3/matches/' + matchData.matchID[indexIter] + '?api_key=' + API_KEY;
    var jsonFinal;
    async.waterfall([
        function(callback) {
              request(individualMatchURL, function(err, response, body) {
                if(!err && response.statusCode == 200) {
                    var json = JSON.parse(body);
                    for (var j = 0; j < 10; j++) {
                        if (matchData.championID[indexIter] == json['participants'][j].championId) {
                            jsonFinal = json['participants'][j];
                            callback(null, jsonFinal);
                        }
                    }
                } 
                else {
                    console.log(err);
                    }
            });
        }
    ],
    function(err, data) {
        if(err) {
            console.log(err);
        }
        else {
            jsonFinal = data;
        }
    });
    console.log(jsonFinal);
    return jsonFinal; 
    }
    

    如何让函数正确返回jsonFinal?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Mikey    8 年前

    function getIndividualMatchJSONObjHelper(matchData, matchParticipantData, indexIter, callback) {
        var individualMatchURL = 'https://na1.api.riotgames.com/lol/match/v3/matches/' + matchData.matchID[indexIter] + '?api_key=' + API_KEY;
        async.waterfall([
            function (callback) {
                request(individualMatchURL, function (err, response, body) {
                    // always trigger the callback even when you have errors or else your program will hang
                    if (err)
                        return callback(err);
                    if (response.statusCode != 200)
                        return callback(new Error('Status code was ' + response.statusCode));
    
                    var json = JSON.parse(body);
                    for (var j = 0; j < 10; j++) {
                        if (matchData.championID[indexIter] == json['participants'][j].championId) {
                            // match found: send back data
                            console.log('inside', json['participants'][j]);
                            return callback(null, json['participants'][j]);
                        }
                    }
                    // if it reaches this point, no match was found
                    callback();
                });
            }
        ], callback); // note: this outer 'callback' is NOT the same as the inner 'callback'
    }
    

    然后当你调用你的函数,例如。

    getIndividualMatchJSONObjHelper(data, participants, indexIter, function (err, json) {
         // you can only get the JSON at this point
         console.log('outside', json);
         matchParticipantData.specificParticipantData[i] = json;
    });