Skip to content

Commit c6550c3

Browse files
Add documentation for JavaScript/TypeScript top-level await support
Document the new top-level await functionality for JavaScript and TypeScript code execution in the Code Interpreter. This feature allows users to write async code without manually wrapping it in async IIFEs, and automatically returns the last expression's value. Related to cloudflare/sandbox-sdk#261 Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent bed8245 commit c6550c3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,38 @@ console.log(result.results[0].text); // "15"
9090
</TypeScriptExample>
9191
:::
9292

93+
**Top-level await (JavaScript/TypeScript)**:
94+
95+
JavaScript and TypeScript code automatically supports top-level `await` without requiring async function wrappers. The last expression's value is returned as the result.
96+
97+
<TypeScriptExample>
98+
```
99+
const ctx = await sandbox.createCodeContext({ language: 'javascript' });
100+
101+
const result = await sandbox.runCode(`
102+
const response = await fetch('https://api.example.com/data');
103+
const data = await response.json();
104+
data
105+
`, { context: ctx });
106+
107+
console.log(result.results[0].json); // Parsed response data
108+
```
109+
</TypeScriptExample>
110+
111+
Simple expressions are automatically returned:
112+
113+
<TypeScriptExample>
114+
```
115+
const ctx = await sandbox.createCodeContext({ language: 'javascript' });
116+
117+
await sandbox.runCode('const x = 10', { context: ctx });
118+
await sandbox.runCode('const y = 20', { context: ctx });
119+
120+
const result = await sandbox.runCode('x + y', { context: ctx });
121+
console.log(result.results[0].text); // "30"
122+
```
123+
</TypeScriptExample>
124+
93125
**Error handling**:
94126

95127
<TypeScriptExample>

0 commit comments

Comments
 (0)