Je rencontre des problèmes pour définir une variable ou au moins la renvoyer dans une cascade asynchrone. Je sais que vous ne pouvez pas retourner en asynchrone mais j'ai fait un rappel sur ma variable, jsonFinal
et il va dans la fonction ci-dessous sous données. Comment puis-je obtenir la fonction de renvoyer jsonFinal correctement?Comment définir une cascade asynchrone variable?
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;
}
Merci pour la réponse. Mais le problème est que lorsque je stocke cela dans une variable, je reste indéfini à l'impression. Êtes-vous sûr qu'il retourne ce rappel? Voici comment la variable est stockée: 'matchParticipantData.specificParticipantData [i] = getIndividualMatchJSONObjHelper (MatchData, matchParticipantData, i, fonction (err, JSON) {});' –
Encore une fois * vous ne pouvez obtenir la variable dans la fonction de rappel comme avec toute opération asynchrone *. Votre fonction ne renvoie rien, mais elle offre un callback avec un argument 'json' quand son opération async est terminée. Vérifié. – Mikey
Merci beaucoup! Vous m'avez aidé à mieux comprendre les rappels. Mais je ne suis préoccupé que par une chose. J'ai remarqué que l'impression de l'objet 'matchParticipantData.specificParticipantData [i]' ne contient pas le json après la fonction. J'ai testé cela avec une variable qui a changé dans la fonction mais pas en dehors de celle-ci. Y a-t-il une raison à cela? Pendant que le JSON était réglé, j'ai besoin qu'il soit aussi à l'extérieur de la fonction. –