ReactiveX / IxJS

The Interactive Extensions for JavaScript
https://reactivex.io/IxJS/
MIT License
1.32k stars 73 forks source link

merge function can cause unhandled rejected promise condition #353

Closed andreasdamm closed 8 months ago

andreasdamm commented 1 year ago

IxJS version: 5.0.0

Code to reproduce:

import { AsyncSink, merge, empty } from "ix/asynciterable/index.js";
async function main() {
    const s = new AsyncSink();
    s.write(42);
    s.error(new Error("error"));
    const m = merge(s, empty());
    for await (const _ of m) {
        break;
    }
}
try {
    await main();
}
catch (e) { }

Expected behavior: Process runs to completion with success exit code and no output

Actual behavior: Process exits prematurely with error exit code due to unhandled promise rejection

Additional information: The [Symbol.asyncIterator] function of MergeAsyncIterable creates a new promise and then yields execution. Should the promise be rejected before execution resumes then no continuation is wired up for that promise causing an unhandled promise rejection which with newer versions of nodejs causes the entire process to exit.

The fix is to first yield the value and then create the new promise.