Skip to content

Conversation

@whoiskatrin
Copy link
Collaborator

@whoiskatrin whoiskatrin commented Nov 25, 2025

top level await, i played a lot with just wrapping all code and tbh, i wasn't happy with it, felt hacky

so now we parse code with acord, then we check if last AST node is an ExpressionStatement, if the answer is yes: wrap code with return on last expression, if no: wrap code without return (returns undefined)

@changeset-bot
Copy link

changeset-bot bot commented Nov 25, 2025

🦋 Changeset detected

Latest commit: e4aa98a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/sandbox Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

agents-git-bot bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Nov 25, 2025
Clarifies that JS/TS code returning Promises is automatically awaited,
ensuring users understand they receive resolved values in results.

Related to cloudflare/sandbox-sdk#246
Copy link
Contributor

@claude claude bot left a comment

Choose a reason for hiding this comment

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

Claude Code Review

The Promise resolution logic is correct and consistently applied to both executors. The thenable check properly detects Promises and the await is correctly placed inside the try-catch block.

Two issues need addressing:

  1. Missing test for rejected Promises - The E2E tests cover 6 scenarios for resolved Promises but zero for rejected ones. Need tests for Promise.reject() and async functions that throw to verify error handling works correctly.

  2. TypeScript standards violation - Using (result as any).then violates CLAUDE.md's "never use any" rule. Should define a proper Thenable interface with a type guard function instead.

See inline comments for specific fixes.

Verdict: Needs fixes before merge

claude[bot]

This comment was marked as outdated.

agents-git-bot bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Nov 25, 2025
Add documentation for automatic Promise resolution in JavaScript and
TypeScript code contexts. This clarifies the behavior fixed in PR #246
where async functions and Promise.resolve() now correctly return their
resolved values instead of empty objects.

Changes:
- Add async code execution examples to API reference
- Add async error handling examples
- Add practical async/await guide section
- Document that Promises are automatically awaited

Related: cloudflare/sandbox-sdk#246
@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 25, 2025

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/sandbox-sdk/@cloudflare/sandbox@246

commit: 2b7481a

@github-actions
Copy link
Contributor

github-actions bot commented Nov 25, 2025

🐳 Docker Image Published

FROM cloudflare/sandbox:0.0.0-pr-246-97c9b17

Version: 0.0.0-pr-246-97c9b17

You can use this Docker image with the preview package from this PR.

Fixes the JavaScript and TypeScript executors to properly await Promises during context execution.
@whoiskatrin whoiskatrin marked this pull request as ready for review November 25, 2025 20:09
claude[bot]

This comment was marked as outdated.

claude[bot]

This comment was marked as outdated.

agents-git-bot bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Nov 25, 2025
Adds documentation for automatic Promise resolution in JavaScript and
TypeScript contexts. The Code Interpreter now properly awaits Promise
results, allowing async/await patterns to work correctly.

Includes examples for:
- Async IIFE patterns
- Direct Promise.resolve usage
- Nested async operations
- Async error handling with Promise.reject

References cloudflare/sandbox-sdk#246
// Async/Await Promise Handling (Issue #206)
// ============================================================================

test('should resolve async IIFE and return the value', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

Just to validate - all the tests are wrapped in an IIFE. Would this change work if the user passed code not wrapped in an IIFE? If not, is the answer then to wrap the underlying implementation in an async IIFE?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@ghostwriternr to support top-level await we'd need to wrap the code in an async IIFE, which adds complexity (need to handle "last expression" capture for multi-statement code) and changes. For this PR, I'd suggest keeping as is since it solves the issue. We can open a separate issue for top-level await? or should we just do it properly here?

agents-git-bot bot pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Nov 25, 2025
Adds practical examples of async JavaScript/TypeScript execution to the
how-to guide, demonstrating data processing workflows with Promises.

References cloudflare/sandbox-sdk#246
@whoiskatrin
Copy link
Collaborator Author

going to start fresh to do the top level await, not just this fix

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants