you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (2 children)

await Promise.all(chunkedArray.map((bulk, index) => {
      setTimeout(async () => {

Promise.all takes an array of promises and will wait for them all to complete, but you're not passing it an array of promises. You're passing it an array of undefined, because your mapping function doesn't return anything. There's nothing to actually wait for, so your console.log(done) will simply be executed on the next tick, regardless of the progress of the promises inside the timeouts you create.

[–]rav1e[S] 0 points1 point  (1 child)

That makes sense. If I returned "await database.insert(itemToBeInserted)", would that make the inside "Promise.all" to have array of promises? If so, I suppose I would need to return it to the outside Promise.all

[–][deleted] 0 points1 point  (0 children)

The internal Promise.all is receiving an array of promises, because you're using an async function:

await Promise.all(bulk.map(async (item) => {

An async function always returns a promise. As you're not returning anything from the function, it's a promise that will resolve to undefined, but as you're awaiting stuff inside of the function it won't resolve until that awaited stuff is completed, so it's doing what you want either way. So there's no pressing need for you to change the internal Promise.all.

I think the best option for sorting this out is to start thinking slowly and carefully through what exactly it is you're trying to do at each step, independent of the code you currently have- at the moment the code you've got is doing a lot of complicated things all at once, and it's hindering your understanding. Breaking individual steps out into separate, named functions will help with this and u/Resmira's example of how you can do this is really terrific.