Skip to content

Nodejs task in promise all persists across new execution #153

Open
@cpalessio96

Description

@cpalessio96

I have a problem with nodejs and promise all.
In my handler i have 2 promises in a promise all, when one of them fails, I catch the error, send it to the client and throw the same error.
In the new execution, before starting the handler, I see that the other promise runs and finishes the task.
Is this a bug? Is this expected behavior?
To make things easier, I created a sample code to reproduce this.

async function aFunc() {
  await new Promise((r) => setTimeout(r, 1000));
  console.log("a function log");
  throw new Error("error by function a");
}

async function bFunc() {
  await new Promise((r) => setTimeout(r, 3000));
  console.log("b function log");
}

export async function handler() {
  try {
    await Promise.all([aFunc(), bFunc()]);
    console.log("end");
  } catch(err) {
    console.log("error promise", err);
    throw err;
  }
}

after 2 execution this is the logs
image

Idk if that is an expected behavior but this cause many side effect.
Is caused by the event loop queue not emptying? is that normal?

Possible Solution

The easiest solution is use promise all settled waited all promises and throw error if there are some rejections but if I want to stop the execution there is a way?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions