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

未关闭
#67,878 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
54/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
领域
api, backend

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.
主要语言
JavaScript
星标
124k
派生
38k
平均合并
4 天 2 小时
30 天内合并 PR
266

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

nodejs/node 的其他 Issue

查看 nodejs/node 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。