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;
});