0
How to store a value from fetch request in another outside variable in javascript
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);
+ 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
});
0
Your question is not that clear eleborate
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.
0
hey Sadaam Linux
thx man
it work for me
ššš