let jsonCompleto
fetch('uno.json')
.then(function (response) {
return response.json();
})
.then(function (data) {
todo = data.length
console.log(todo)
jsonCompleto = data
console.log(jsonCompleto) // return all the json file correctly
})
// how can avoid this as undefined
console.log(jsonCompleto) // return undefined
how can load only once the json file and use his all content in another things ?
ilenia
November 7, 2021, 5:39pm
2
put a =
here and a return statement in the various chained functions
sorry, can you show me the code and explain me how to, can I load only once the json file, and use all the content file outside the function ?
eoja
November 9, 2021, 1:09am
4
You are only fetching the data once.
async
and await
can help you with your problem.
The code is not waiting for the fetch
to return data and keeps on executing the next thing which is
and executes before the fetch gets the data so it logs undefined because it is well, yet undefined.
Promises are the foundation of asynchronous programming in modern JavaScript. A promise is an object returned by an asynchronous function, which represents the current state of the operation. At the time the promise is returned to the caller, the...
The Fetch API provides a JavaScript interface for accessing and manipulating parts of the protocol, such as requests and responses. It also provides a global fetch() method that provides an easy, logical way to fetch resources asynchronously across...
system
Closed
May 10, 2022, 1:09pm
5
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.