Skip to content

forEachChild is hard to use with async API #64126

Open

Description

If you try to use the async API, it's a challenge today. The API provides a synchronous tree traversal function (forEachChild), but pulling on semantic information is asynchronous.

The problem isn't the mixture itself - it's that forEachChild stops walking when the visitor function returns a truthy result. This makes it impractical to use with async APIs because every async function returns a Promise which is truthy.

The current workarounds are a little awkward, basically introducing a layer of indirection between the traversal function and forEachChild that uses intermediate queues.

I'm not sure if there's an ideal solution - there's at least two problems right now:

  • it feels like we could benefit from a forEachChildAsync or something like that.
  • using forEachChild is a foot-gun if you've decided to use the async API.

Activity

  1. TyceHerrman commented on Nov 9, 2026

    @TyceHerrman

    #64302 added childrenIter(), providing an alternative to callback-based forEachChild that works with async traversal. I think that mostly resolves this issue other than the concern about forEachChild being an async foot-gun, but that could be split out into a new issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Domain: APIRelates to the public API for TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions