0👍
✅
Quite difficult to follow with so much async
/await
going on. A bit of refactoring to deal with the pyramid of doom wouldn’t hurt.
This line catches my eye:
return response.data.map(async (val, index) => {
This will be returning an array of unresolved promises. The surrounding function, getSessions
, is async
so it will wrap the return value in a further promise.
The line await this.getSessions();
will wait for that promise to resolve. Unfortunately it’ll resolve immediately to the array of promises, without waiting for the individual promises to resolve. This is why the logging appears to be one step behind, as the inner promises haven’t finished yet.
I think what you need is to add Promise.all
, such that the outer promise waits for the array of promises.
return Promise.all(response.data.map(async (val, index) => {
Source:stackexchange.com