Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When we throw with batchWithTimeout loop is not finalized #294

Open
oleg-slapdash opened this issue Jul 5, 2023 · 0 comments
Open

When we throw with batchWithTimeout loop is not finalized #294

oleg-slapdash opened this issue Jul 5, 2023 · 0 comments

Comments

@oleg-slapdash
Copy link

When an async generator wrapped with batchWithTImeout is never "finalized", while batch and naked loop is.

Please see examples below, both "batch" and simple loop prints Loop Finally while batchWithTimeout does not print Loop Finally and continues reading from the stream.

Simple loop:

 async function* generate() {
    try {
      for (let i = 0; i < 20; i++) {
        console.log({ i });
        yield i;
        await new Promise((resolve) => setTimeout(resolve, 10));
      }

      console.log("Loop Completed");
    } catch (err) {
      console.log("Loop Caught error", err);
      throw err;
    } finally {
      console.log("Loop Finally");
    }
  }

  try {
    for await (const i of generate()) {
      console.log({ i });
      if (i === 10) {
        console.log("Thrown");
        throw new Error("Thrown");
      }
    }

    console.log("Global Completed");
  } catch (err) {
    console.log("Global Caught error", err);
    throw err;
  } finally {
    console.log("Global Finally");
  }

Output

{ i: 0 }
{ i: 0 }
{ i: 1 }
{ i: 1 }
{ i: 2 }
{ i: 2 }
{ i: 3 }
{ i: 3 }
{ i: 4 }
{ i: 4 }
{ i: 5 }
{ i: 5 }
{ i: 6 }
{ i: 6 }
{ i: 7 }
{ i: 7 }
{ i: 8 }
{ i: 8 }
{ i: 9 }
{ i: 9 }
{ i: 10 }
{ i: 10 }
Thrown
Loop Finally
Global Caught error Error: Thrown

Loop with a batch:

async function* generate() {
  try {
    for (let i = 0; i < 20; i++) {
      console.log({ i });
      yield i;
      await new Promise((resolve) => setTimeout(resolve, 10));
    }

    console.log("Loop Completed");
  } catch (err) {
    console.log("Loop Caught error", err);
    throw err;
  } finally {
    console.log("Loop Finally");
  }
}

try {
  for await (const chunk of batch(3, generate())) {
    console.log({ chunk });
    if (chunk.includes(10)) {
      console.log("Thrown");
      throw new Error("Thrown");
    }
  }

  console.log("Global Completed");
} catch (err) {
  console.log("Global Caught error", err);
  throw err;
} finally {
  console.log("Global Finally");
}

Output

{ i: 0 }
{ i: 1 }
{ i: 2 }
{ chunk: [ 0, 1, 2 ] }
{ i: 3 }
{ i: 4 }
{ i: 5 }
{ chunk: [ 3, 4, 5 ] }
{ i: 6 }
{ i: 7 }
{ i: 8 }
{ chunk: [ 6, 7, 8 ] }
{ i: 9 }
{ i: 10 }
{ i: 11 }
{ chunk: [ 9, 10, 11 ] }
Thrown
Loop Finally
Global Caught error Error: Thrown
async function* generate() {
  try {
    for (let i = 0; i < 20; i++) {
      console.log({ i });
      yield i;
      await new Promise((resolve) => setTimeout(resolve, 10));
    }

    console.log("Loop Completed");
  } catch (err) {
    console.log("Loop Caught error", err);
    throw err;
  } finally {
    console.log("Loop Finally");
  }
}

try {
  for await (const chunk of batchWithTimeout(3, 1000000, generate())) {
    console.log({ chunk });
    if (chunk.includes(10)) {
      console.log("Thrown");
      throw new Error("Thrown");
    }
  }

  console.log("Global Completed");
} catch (err) {
  console.log("Global Caught error", err);
  throw err;
} finally {
  console.log("Global Finally");
}

Output

{ i: 0 }
{ i: 1 }
{ i: 2 }
{ chunk: [ 0, 1, 2 ] }
{ i: 3 }
{ i: 4 }
{ i: 5 }
{ chunk: [ 3, 4, 5 ] }
{ i: 6 }
{ i: 7 }
{ i: 8 }
{ chunk: [ 6, 7, 8 ] }
{ i: 9 }
{ i: 10 }
{ i: 11 }
{ chunk: [ 9, 10, 11 ] }
Thrown
Global Caught error Error: Thrown
{ i: 12 }
@oleg-slapdash oleg-slapdash changed the title When we throw with batchWithTimeout loops keeps dangling. When we throw with batchWithTimeout loop is not finalized Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant