Skip to content

Add defer functionality to generator executor - #64157

Merged
Wesley Wigham (weswigham) merged 4 commits into
microsoft:mainfrom
weswigham:generator-defer
Sep 8, 2026
Merged

Add defer functionality to generator executor#64157
Wesley Wigham (weswigham) merged 4 commits into
microsoft:mainfrom
weswigham:generator-defer

Conversation

@weswigham

Copy link
Copy Markdown
Member

Add a defer function and support to the api.batch executor for it, allowing you to queue work to run within the current batching context without blocking execution of the current active generator - essentially the generator equivalent of an unawaited async function call in the async API.

cc Titian Cernicova-Dragomir (@dragomirtitian) who specifically requested built-in support for this, since it's cumbersome to wire up without owning the generator executor, but when you own it, it's comparably not bad to add.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Deferred failures can prematurely interrupt foreground work, and completed generators cause quadratic scanning.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds deferred generator work to synchronous API batching without returning deferred results.

Changes:

  • Adds defer and deferred-work scheduling.
  • Refactors request execution, deduplication, and result mapping.
  • Adds runtime coverage for deferred execution and errors.
File summaries
File Description
packages/typescript/src/api/sync/generatorSupport.ts Implements deferred request execution.
packages/typescript/src/api/sync/api.ts Exposes defer through synchronous batching.
packages/typescript/src/api/async/api.ts Updates sync-generation directives.
packages/typescript/test/sync/api-generators.test.ts Tests batching and deferred behavior.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 2
  • Review effort level: Balanced

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread packages/typescript/src/api/sync/generatorSupport.ts
Comment thread packages/typescript/src/api/sync/generatorSupport.ts
Comment thread packages/typescript/src/api/sync/api.ts Outdated
export { all, defer } from "./generatorSupport.ts";
import {
all,
type APIRequestGenerator,

@dragomirtitian Titian Cernicova-Dragomir (dragomirtitian) Sep 4, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we expose UndeferredAPIRequestGenerator and APIRequestGenerator. If the generators are recursive they need a type. We could redefine some of these types on the usage side but they are now more complicated that a simple generator.

If we expose it, I'm not sure UndeferredAPIRequestGenerator is the best name for the common case of the generator, and this is the type that is needed as the return type since APIRequestGenerator is a union and does not work well as a generator return type:

function *testNotOk(n: number): sync.APIRequestGenerator<number> {
    if(n === 0) return 0;
    return yield * testNotOk(n-1)
}


function *testOk(n: number): sync.UndeferredAPIRequestGenerator<number> {
    if(n === 0) return 0;
    return yield * testOk(n-1)
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've renamed 'em and exported 'em. AnyAPIRequestGenerator is the union, APIRequestGenerator is the undeferred one, and DeferredAPIRequestGenerator remains the deferred one.

@github-project-automation github-project-automation Bot moved this from Not started to Needs merge in PR Backlog Sep 8, 2026
@weswigham
Wesley Wigham (weswigham) added this pull request to the merge queue Sep 8, 2026
Merged via the queue into microsoft:main with commit 10404f7 Sep 8, 2026
26 checks passed
@weswigham
Wesley Wigham (weswigham) deleted the generator-defer branch September 8, 2026 21:04
@github-project-automation github-project-automation Bot moved this from Needs merge to Done in PR Backlog Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants