-
Notifications
You must be signed in to change notification settings - Fork 30k
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
docs: writable streams from async iterators example issue #31222
Comments
@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 |
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. |
@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 |
The for await loop into writable loop could cause an unhandled exception in the case where we are waiting for data from the async iterable and this no `'error'` handler is registered on the writable. Fixes: nodejs#31222
The for await loop into writable loop could cause an unhandled exception in the case where we are waiting for data from the async iterable and this no `'error'` handler is registered on the writable. Fixes: #31222 PR-URL: #31252 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
The for await loop into writable loop could cause an unhandled exception in the case where we are waiting for data from the async iterable and this no `'error'` handler is registered on the writable. Fixes: #31222 PR-URL: #31252 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
The for await loop into writable loop could cause an unhandled exception in the case where we are waiting for data from the async iterable and this no `'error'` handler is registered on the writable. Fixes: #31222 PR-URL: #31252 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
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
writable
emits an'error'
before the first chunk arrives from theiterator
, e.g. ifwritable
is afs
stream and it fails while opening the file.The text was updated successfully, but these errors were encountered: