2017-07-17 2 views
0

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

Répondre

1

Vous pouvez uniquement obtenir la variable dans le rappel comme avec n'importe quelle opération asynchrone. Par conséquent, modifiez votre fonction pour accepter également un rappel.

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' 
} 

Ensuite, lorsque vous appelez votre fonction, par ex.

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

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) {});' –

+0

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

+0

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. –