Skip to content

Commit 00c38c0

Browse files
Document top-level await support for JavaScript code execution
Adds documentation for the new top-level await feature in JavaScript code interpreter contexts. This enhancement allows users to write async code without manual IIFE wrapping. Changes: - Add top-level await section to code-execution.mdx guide - Update AI-generated code example to showcase async patterns - Add JavaScript top-level await documentation to interpreter.mdx API reference - Explain automatic async wrapping and expression return behavior Synced from cloudflare/sandbox-sdk PR #261 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 4a2fab8 commit 00c38c0

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

src/content/docs/sandbox/api/interpreter.mdx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ const result = await sandbox.runCode(code: string, options?: RunCodeOptions): Pr
6060
- `error` - Execution error if any
6161
- `executionCount` - Execution counter
6262

63+
**JavaScript top-level await**:
64+
65+
JavaScript code automatically supports top-level await without requiring async function wrappers. The last expression's value is automatically returned:
66+
67+
<TypeScriptExample>
68+
```
69+
const ctx = await sandbox.createCodeContext({ language: 'javascript' });
70+
71+
// Top-level await works automatically
72+
const result = await sandbox.runCode(`
73+
const users = await fetch('/api/users').then(r => r.json());
74+
users.length
75+
`, { context: ctx });
76+
77+
console.log(result.results[0].text); // Number of users
78+
```
79+
</TypeScriptExample>
80+
6381
**Recommended usage - create explicit context**:
6482

6583
<TypeScriptExample>

src/content/docs/sandbox/guides/code-execution.mdx

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,32 @@ console.log('Success:', result.success);
7474
```
7575
</TypeScriptExample>
7676

77+
### Top-level await in JavaScript
78+
79+
JavaScript code execution supports top-level await without requiring manual async function wrapping:
80+
81+
<TypeScriptExample>
82+
```
83+
const context = await sandbox.createCodeContext({
84+
language: 'javascript'
85+
});
86+
87+
// Top-level await works directly
88+
const result = await sandbox.runCode(`
89+
const response = await fetch('https://api.example.com/data');
90+
const data = await response.json();
91+
data
92+
`, { context: context.id });
93+
94+
console.log('Result:', result.results[0]); // The last expression value
95+
```
96+
</TypeScriptExample>
97+
98+
The code interpreter automatically:
99+
- Wraps code in an async context to enable top-level await
100+
- Returns the value of the last expression when it is a standalone expression
101+
- Handles Promise resolution transparently
102+
77103
### State within a context
78104

79105
Variables and imports remain available between executions in the same context, as long as the container stays active:
@@ -185,7 +211,7 @@ print("Done!")
185211

186212
## Execute AI-generated code
187213

188-
Run LLM-generated code safely in a sandbox:
214+
Run LLM-generated code safely in a sandbox. JavaScript code automatically supports top-level await, making it ideal for AI-generated async patterns:
189215

190216
<TypeScriptExample>
191217
```
@@ -202,20 +228,20 @@ const response = await fetch('https://api.anthropic.com/v1/messages', {
202228
max_tokens: 1024,
203229
messages: [{
204230
role: 'user',
205-
content: 'Write Python code to calculate fibonacci sequence up to 100'
231+
content: 'Write JavaScript code to fetch user data from an API'
206232
}]
207233
})
208234
});
209235
210236
const { content } = await response.json();
211-
const code = content[0].text;
237+
const code = content[0].text; // e.g., "const data = await fetch(...); data"
212238
213-
// 2. Execute in sandbox
214-
const context = await sandbox.createCodeContext({ language: 'python' });
239+
// 2. Execute in sandbox - top-level await works automatically
240+
const context = await sandbox.createCodeContext({ language: 'javascript' });
215241
const result = await sandbox.runCode(code, { context: context.id });
216242
217243
console.log('Generated code:', code);
218-
console.log('Output:', result.output);
244+
console.log('Result:', result.results[0]);
219245
console.log('Success:', result.success);
220246
```
221247
</TypeScriptExample>

0 commit comments

Comments
 (0)