Skip to content

docs: writable streams from async iterators example issue #31222

@ronag

Description

@ronag

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 the iterator, e.g. if writable is a fs stream and it fails while opening the file.

  const writable = fs.createWriteStream('./file');

  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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions