简体   繁体   中英

How to store fetched data from API to Global Variable

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM