0

How to store a value from fetch request in another outside variable in javascript

27th Dec 2022, 10:37 PM
Zos Zos
Zos Zos - avatar
6 Answers
+ 5
you can use the following steps: Declare the outside variable before the fetch request, so that it is available in the scope of the fetch request. let outsideVariable; In the fetch request, use the .then() method to access the response data and store it in the outside variable. fetch(url) .then(response => response.json()) .then(data => { outsideVariable = data; }); You can then use the outsideVariable anywhere in your code, as it now contains the value from the fetch request. console.log(outsideVariable);
28th Dec 2022, 8:36 AM
Sadaam Linux
Sadaam Linux - avatar
+ 4
Note that the value of the outsideVariable will not be available immediately after the fetch request is made, as the fetch request is asynchronous. You will need to use the .then() method to access the value of the outsideVariable after the fetch request has completed. fetch(url) .then(response => response.json()) .then(data => { outsideVariable = data; }) .then(() => { console.log(outsideVariable); // value of outsideVariable is now available });
28th Dec 2022, 8:36 AM
Sadaam Linux
Sadaam Linux - avatar
0
Your question is not that clear eleborate
27th Dec 2022, 11:42 PM
Vanessa Nilsson
0
The way to store variables persistently is to use a post request provided by the backend to post the data to the backend, which can then store the data in the backend database.
28th Dec 2022, 9:36 AM
CalviÕ²
CalviÕ² - avatar
0
hey Sadaam Linux thx man it work for me šŸ˜˜šŸ˜˜šŸ˜˜
13th Feb 2024, 2:02 PM
Hamid Reza Heydari
Hamid Reza Heydari - avatar