Skip to content
Advertisement

Promise resolving too early

I’m having an issue where my Promise.all is resolving too early. For a test I want to console.log the length of the array which is getting pushed from within the promise map but it is returning 0 sadly. I’m sure it’s something simple…

JavaScript

Any help would be greatly appreciated.

Advertisement

Answer

There are a lot of issues there. Mainly they fall into these categories:

  • Not returning the results of promise chains from fulfillment handlers, which means the chain the fulfillment handler is in won’t be linked up to the promise chain you created within it
  • Calling functions and passing their return value into then, rather than passing a function into then

See inline comments in this minimal, least-changes reworking of that code:

JavaScript

FWIW, though, if I had to not use async functions I’d probably do it like this:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement