I have one API
where the request is made from a file Javascript
using fetch
.
The issue or the problem is that sometimes there is a lot of data that I need to show and that takes a while to load and even knowing that he is doing the process below, sometimes I doubt if he is executing or not, so I would like that while he API fetch
does everything the process, first a spinner effect of a gif, png or a design with CSS
whatever, the only thing I would like is to know how to do it, what to use and the rest I could do.
CODE (it's an example not the data I'm using):
fetch("https://api.covid19api.com/summary")
.then((response) => response.json())
.then((datos) => {
var cov19 = datos.Global
cadena = `<tr>
<td><input type="number" value="${cov19.NewConfirmed}"></td>
<td><input type="number" value="${cov19.TotalConfirmed}"></td>
<td><input type="number" value="${cov19.NewDeaths}"></td>
<td><input type="number" value="${cov19.TotalDeaths}"></td>
<td><input type="number" value="${cov19.NewRecovered}"></td>
<td><input type="number" value="${cov19.TotalRecovered}"></td>
</tr>`
document.getElementById('generar_datos').innerHTML= cadena
})
.catch(error => console.error('Error:', error));
<div class="row">
<table class="table table-responsive table-borderd table-sm">
<thead class="thead-dark">
<tr>
<th>Nuevos Confirmados</th>
<th>Total Confirmados</th>
<th>Nuevos Fallecidos</th>
<th>Total Fallecidos</th>
<th>Nuevos Recuperados</th>
<th>Recuperados</th>
</tr>
</thead>
<tbody id="generar_datos"></tbody>
</table>
</div>
The idea is almost the same I use
fetch
get and then display data but in a different way (I usefor
andforeach
, I don't know if that makes a difference). what I am showing (code) is an example is not what I am actually doing, it was the first thing that came to my mind.
I hope you can help me with this question. Thank you!
You can use
await
and control in a clear and simple way when the data load has finished, putting a Spinner or some text that informs the user and hiding it once the data has been loaded.If you want to keep using promises instead of using
async
andawait
:You just have to add a loading style and it will depend on where you want it to look from a gift to a vector image, and in addition to this the trick for this is to use the .then() features .
The idea of using .finally() is in case the service fails, we also return the styles as they were
You can simply have an element that contains your load element, be it a gif or an animation with CSS (as in the example) and what you do is keep it hidden with
display: none
that should be its default state, then when you execute your function to fetch data just before it runs you change the property ofdisplay
to something visible, in this caseflex
, but it could just as well be one of the other values ofdisplay
. Once the load is finished you hide your container from the animation again.For your case you would have to wrap
fetch
in a function like this:Here is a working example
I update @Sergio's answer.
You always have to deal with the error case .
Defining the following functions,
The main function would be as follows.
with syntax
then
andcatch
And finally, for note. Use a decorator pattern for logic separation.
I hope it works.