0

I'm using a a js lib called teemojs to get information from riot API using node.js and want to store what i get in a var to call back to later, this code

api.get('euw1', 'summoner.getBySummonerName', playerName)
    .then(data => console.log(data.id))

gives me what I want but I'm not able to store it in a var to access globally any ideas on what i can do

P.S I'm trying to say something like this

api.get('euw1', 'summoner.getBySummonerName', playerName)
    .then(data => var Result = (data.id))
Luke Noot
  • 1
  • 1
  • The results of an asynchronous operation needs to be used INSIDE the `.then()` handler or in a function you call from there. The timing of the async completion is completely unknown so the only place you know when it's safe to use the value is inside the `.then()` handler. That's how async development works in node.js. You can store stuff into a global context, but you will have a hard time knowing when it's safe to use it from there. – jfriend00 Feb 19 '18 at 19:49
  • Can you link me a example on how to use async/await? – Luke Noot Feb 19 '18 at 19:52
  • The question yours has been marked a duplicate of has numerous examples in it. There are also hundreds of articles and tutorials on the web on the subject which you can find as easily as I. It is a well publicized and written about new feature of Javascript. – jfriend00 Feb 19 '18 at 20:01
  • Can I just say that this wrapper being called "teemojs" makes me happy inside? That's all. – Jacob H Feb 19 '18 at 21:02

1 Answers1

1

You have to declare a variable before your promise like

var myVar;
api.get('euw1', 'summoner.getBySummonerName', playerName)
    .then(data => {
       myVar = data.id;
       console.log(myVar); // myVar is defined
    })
console.log(myVar); // myVar is undefined

You can also use async/await like

ty {
  const {id} = await api.get('euw1', 'summoner.getBySummonerName', playerName);
  console.log(id);
} catch (e) {
  console.error(e);
}
Striped
  • 2,544
  • 3
  • 25
  • 31
  • doesn't work, if i call console.log(myVar) comes back "undefined" – Luke Noot Feb 19 '18 at 19:41
  • Did you console log in the `then` or outside ? 'Cause outside the promise is not resolved. – Striped Feb 19 '18 at 19:42
  • I'm trying to do it outside but I've never used promises – Luke Noot Feb 19 '18 at 19:43
  • Take a look at y edit. https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Promise – Striped Feb 19 '18 at 19:43
  • So i cant get the "data.id" outside of the promise? – Luke Noot Feb 19 '18 at 19:46
  • Yes, with a new promise, but it will be the same though. You will have to wrap your logic in a new promise if you want `myVar` defined. – Striped Feb 19 '18 at 19:47
  • I've added the code you gave me and it works but I still dont see how I can make myVar global? do i need for everything to be in the brackets of the try? – Luke Noot Feb 19 '18 at 20:00
  • That's it. You have to wrap your logic where you need `myVar` once the promise is resolved. As wrote @jfriend00 `The results of an asynchronous operation needs to be used INSIDE the .then() handler or in a function you call from there.` – Striped Feb 19 '18 at 20:02
  • I think you confused poor Luke by showing the correct and incorrect console.log (he probably did both). – Jacob H Feb 19 '18 at 21:04