Unhandled promise rejection when iterating over async calls before awaiting all

Viewed 209

I've encountered a weird problem I cant find an explanation to anywhere. In the following code, I'm trying to get a number of position entities, each by id using and API call.

Some id requests might end up with an 404, and I dont mind that, that's why I'm using Promise.allSettled()

    const positionsPromises = idList.map((id) => {
      return this.positionRepository.getPositionById(id);
    });

    const positions = await Promise.allSettled(positionsPromises);

This generally works, but for non-existing ids (or any other errors), I get the famous node logs for UnhandledPromiseRejectionWarning

however... when taking the same .map() code and just throwing it directly inside Promise.allSettled() I get no such things, and it works as expected- some resolve, some reject, and I can iterate over positions to check each one.

    const positions = await Promise.allSettled(
      idList.map((id) => {
        return this.positionRepository.getPositionById(id);
      }),
    );

Why is this happening? Am I doing something wrong here with returning an array of pending Promises before awaiting them all?

0 Answers
Related