Skip to content
Advertisement

javascript async fetch function

I am trying to create a recursive function that sends a PUT request for every integer of a given array, and call another function at the end of it.

JavaScript

But it seems that it calls the load_mailbox() function before fetching the last item of the array.
I know that this should be better implemented using async / await. Can someone give an example of that to help me understand?

UPDATE: It turns out that the code below is working

JavaScript

Advertisement

Answer

This is the correct code for async for..of loop

JavaScript

However, this code does not work and causes infinity recursion 🙂 I think it is bad idea to mutate the array inside iteration. Also, please keep in mind, that then receives callback. So, the propper argument for then is:

JavaScript

In your case, you can’t pass fetchArchive as an argument to then method because fetchArchive does not return function

[UPDATE]

This is the working code with array index comparison:

JavaScript

Documentation about entries U can find here

Advertisement