Repository navigation
docs: writable streams from async iterators example issue #31222
Description
Activity
@mcollina / @nodejs/streams: I can't think of a elegant way to resolve this. Any ideas?
Otherwise, I would suggest we remove this example until we find a better way in order to avoid people using this pattern and thinking it's safe.
Maybe?
EDIT: Or rather maybe not :D
const writable = fs.createWriteStream('./file'); // catch 'error' while not writing // EDIT: add a noop .catch to avoid unhandledRejection before awaiting. const finishedPromise = finished(writable).catch(() => {}); for await (const chunk of iterator) { if (writable.destroyed) // assumes destroy() on 'error' break; // Handle backpressure on write(). if (!writable.write(chunk)) await once(writable, 'drain'); } writable.end(); // Ensure completion without errors. await finishedPromise;
what is the
const finishedPromise = finished(writable);meaning? 🤔what is the const finishedPromise = finished(writable); meaning? 🤔
it returns a promise that will resolve or reject when the writable finishes or errors.
Actually, that won't work either since we currently do unhandled rejection for rejected promises without a catch.
Reacted by Alex Yang@himself65: "Fixed". It's not good though. Maybe not that one.
Another try. What if we use some kind of event that signals that no error can occur until a write is issued?
const writable = fs.createWriteStream('./file'); await once(writable, 'ready'); // No error possible until writable.write for await (const chunk of iterator) { // Handle backpressure on write(). if (!writable.write(chunk)) await once(writable, 'drain'); } writable.end(); // Ensure completion without errors. await finished(writable).catch(() => {});
Though unfortunately, that does not exist currently. Probably also not possible to enforce since one can call
.destroy(err)at any time. I don't like this either...- added a commit that references this issue
on Jan 10, 2020 - added 2 commits that reference this issue
on Jan 16, 2020
Given the example from https://nodejs.org/api/stream.html#stream_piping_to_writable_streams_from_async_iterators.
There is a problem where this can cause an unhandled exception if the
writableemits an'error'before the first chunk arrives from theiterator, e.g. ifwritableis afsstream and it fails while opening the file.