Skip to content Skip to sidebar Skip to footer

Asyncawait Is Not Waiting For The Fetch Response

I am having a problem in executing API calls using aysnc and await. I am using fetch method to call a Spring Boot service which returns a JSON string after uploading a file. Before

Solution 1:

requestAwait is awaiting fetch, but the caller function isn't awaiting requestAwait.

Also, with several parallel API calls, I think you'll be better off with Promise.all() instead of the async-await recipe.

Solution 2:

The code is not waiting, because const requestAwait is a promise. So when you cast requestAwait() the code continues to file.upload = true, without waiting for the fuction to end.

As it is right now, to make it work You must add ASYNC inside the map here :

acceptedFiles.map( async (file) => { ...function } )

Then before the cast of the requestAwait.

await requestAwait();
file.uploaded = true;
return file;

Post a Comment for "Asyncawait Is Not Waiting For The Fetch Response"