Option to wait for all promises in `Stream.prototype.forEach` queue to finish before rejecting with a thrown error

Aperta
#67,878 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
54/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
javascript, node.js
Ambito
api, backend

Direzione di ricerca

Start by reading the implementation and tests for Readable.prototype.forEach, especially how concurrency and promise rejections are handled. Add an option that waits for already started callbacks to settle and combines additional failures in an AggregateError. Done means tests cover the option with concurrent callbacks that resolve and reject, and verify that forEach settles only after those callbacks finish.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

feature request
What is the problem this feature will solve?

Preventing side effects from running after Stream.prototype.forEach has returned when concurrency is greater than 1 and a promise has rejected (which currently results in forEach immediately rejecting, leaving already started promises running).

In the following example up to 63 existing calls to processAsset may be running if one call throws an exception, resulting in observable side effects.

export async function generateBrotliArtifacts(
  manifests: Manifest[],
  appRootDir: AbsolutePath,
): Promise<void> {
  async function processAsset(assetPath: RelativePath) {
    const chunkBuffer = await fs.readFile(AbsolutePath.join(appRootDir, assetPath));

    // Generate plain brotli artifact for non-batched requests
    const brotliPath = AbsolutePath.join(appRootDir, assetPath + BROTLI_EXT);
    const compressedBrotliData = await compress(chunkBuffer);
    await fs.writeFile(brotliPath, compressedBrotliData);
  }

  await Readable.from(getPrecompressedAssetPaths(manifests))
    .forEach(processAsset, { concurrency: 64 });
}

This is problematic when used in combination with import('node:fs/promises').mkdtempDisposable (disposal occurs while writes can still occur), or with tests where multiple tests cases reuse a common directly (or re-assign with a before callback).

What is the feature you are proposing to solve the problem?

A new option to wait for already started promises to be completed. If additional promise rejections or thrown errors occur, they are bundled up into a single AggregateError (so no error is silently discarded).

A potential further optimisation would be triggering the AbortSignal given to callees (wrapping one provided to forEach with AbortSignal.any()) and discarding expected errors (functions properly implementing the abortable API contract reject with the provided reason). However I consider this to be a separate nice-to-have request that deserves it's own separate option.

What alternatives have you considered?
  • Catching errors and triggering a provided AbortSignal, forEach will still return early.
  • Observing errors (catch and re-throw) and triggering a provided AbortSignal. Can reduce but not eliminate side effects.
Lingua principale
JavaScript
Stelle
124k
Fork
38k
Merge medio
4g 2h
PR unite (30g)
266

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di nodejs/node

Tutte le issue di nodejs/node

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.