The only thing you have to do is:
const arrayDatos = fetch(url).then(res => res.json())
What you are going to store is a promise containing the data obtained through the AJAX request. Keep in mind that fetch
returns a promise with the result of the request; therefore, to access the data you must use then
:
arrayDatos.then((datos) => {
// hacer algo con los datos
})
However, if you want to store the data directly from the request; You can use Asynchronous Functions to wait for the result of the promise:
Note : To make use of this feature the code where await
is called must be in a function marked with async
.
const obtenerDatos = async () => {
const arrayDatos = await fetch(url).then(res => res.json())
arrayDatos[0] // un valor
// resto de código
}
It should be clarified that any function marked with async
returns a promise regardless of whether something is explicitly returned or not.