0

Bear with me as I'm REALLY new to node.js and javascript in general. So, I installed a JavaScript wrapper which gave me classes which can communicate with an API to fetch me information. (I used the LeagueJS wrapper). However, whenever I get the data from the class and then store it in a variable so I can use it in a different class, as soon as I leave the class the variable becomes undefined. I presume this is because I can't use the variable outside of it's scope, is there a way for it to be used? For context, data is an object and id is a Property trying to get the Property value and storing it in the new123 variable. I then want to use the new123 variable as an argument for .gettingLeagueById but cannot achieve this as the variable is undefined. Here's a snippet from my code:

    async run(message, summonID){
    var SummName = summonID;
    api.Summoner
    .gettingByName(SummName, 'euw')
    .then(data => {
        'use strict';
        var new123 = data.id;
    })
    api.League
    .gettingLeagueById(new123)
    .then(data => {
        'use strict';
        console.log(data);
    });
}
Max
  • 1
  • 5
  • Your `data` or `new123` variables are ONLY available within that `.then()` callback function. That is their Javascript scope. They do not exist outside that particular function. So, you can either use them inside that callback, call a function from within the callback and pass that data to the function or you can make it part of the return value of that function so that the data is presented to later `.then()` handlers in your promise chain. See the question yours had been marked a duplicate of for lots more options for sharing the data with later parts of the promise chain. – jfriend00 Nov 09 '17 at 01:13
  • I see what you're talking about and your examples are really good. However, to get the data from the class and it's function I need to use .then to obtain the data and then print it. I'm wondering, is there a way I could get the data from the class without using a .then handler because I can't find a way to obtain the information otherwise. I've tried using callbacks and a function but then the data turns out to be undefined again. – Max Nov 09 '17 at 18:08
  • No. The only way to get data from a promise is with `.then ()`. – jfriend00 Nov 09 '17 at 18:25
  • If that is the only way, does that mean that the promise is already nested inside the function gettingbyName and gettingLeagueById? – Max Nov 09 '17 at 18:28
  • Those functions return a promise. They create the promise, return it and then resolve or reject the promise when the async operation is complete which both sets the value inside the promise and triggers your `.then ()` handler to het called. – jfriend00 Nov 09 '17 at 18:32
  • Even so, how am I supposed to get the information from data into an argument for the function? Doesn't the data stay in the .then() handlers and can't be used outside them? – Max Nov 09 '17 at 19:47
  • You put any code that uses the data inside the `.then()` handler or on a function you calk from there. That's how async programming works in Javascript. – jfriend00 Nov 09 '17 at 19:50

0 Answers0