Home > Blockchain >  How to store fetched data from API to Global Variable
How to store fetched data from API to Global Variable

Time:08-04

As seen in this picture, I have initialized variable madhu globally and assigning the fetched data to this variable inside .then method but it's showing me undefined. How can I store this value globally? picture

CodePudding user response:

You need to understand the difference between synchronous and asynchronous code. The value is setting correctly as in line 160 after response is received but line 167 runs before 160 and at that time the value was not set.

CodePudding user response:

You can do it in different options.

The simplest is using localStorage already in the HTML5, you could save the data in the browser

localStorage.setItem('response', JSON.stringify(data));
myData = JSON.parse(localStorage.getItem('response'));

sorry my english is not very good, I hope I have explained myself well

  • Related