Skip to content

Commit 7831294

Browse files
Document async/await support in JS/TS code execution
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
1 parent ad91c85 commit 7831294

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

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

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

93+
**Async code execution (JavaScript/TypeScript)**:
94+
95+
JavaScript and TypeScript contexts automatically await Promise results:
96+
97+
<TypeScriptExample>
98+
```
99+
const ctx = await sandbox.createCodeContext({ language: 'javascript' });
100+
101+
// Async IIFE - returns resolved value
102+
const result1 = await sandbox.runCode(
103+
'(async () => { return 42; })()',
104+
{ context: ctx }
105+
);
106+
console.log(result1.results[0].json); // 42
107+
108+
// Promise.resolve - returns resolved value
109+
const result2 = await sandbox.runCode(
110+
'Promise.resolve({ status: "success", value: 123 })',
111+
{ context: ctx }
112+
);
113+
console.log(result2.results[0].json); // { status: "success", value: 123 }
114+
115+
// Nested async operations
116+
const result3 = await sandbox.runCode(`
117+
(async () => {
118+
const a = await Promise.resolve(10);
119+
const b = await Promise.resolve(20);
120+
return a + b;
121+
})()
122+
`, { context: ctx });
123+
console.log(result3.results[0].json); // 30
124+
```
125+
</TypeScriptExample>
126+
93127
**Error handling**:
94128

95129
<TypeScriptExample>
@@ -104,6 +138,25 @@ if (result.error) {
104138
```
105139
</TypeScriptExample>
106140

141+
**Async error handling (JavaScript/TypeScript)**:
142+
143+
<TypeScriptExample>
144+
```
145+
const ctx = await sandbox.createCodeContext({ language: 'javascript' });
146+
147+
// Rejected promises are caught as errors
148+
const result = await sandbox.runCode(
149+
'Promise.reject(new Error("Failed"))',
150+
{ context: ctx }
151+
);
152+
153+
if (result.error) {
154+
console.error(result.error.message); // Error details
155+
console.error(result.logs.stderr); // Stack trace
156+
}
157+
```
158+
</TypeScriptExample>
159+
107160
### `listCodeContexts()`
108161

109162
List all active code execution contexts.

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,51 @@ print("Done!")
183183
```
184184
</TypeScriptExample>
185185

186+
## Execute async JavaScript/TypeScript
187+
188+
JavaScript and TypeScript contexts automatically await Promise results, making async code execution seamless:
189+
190+
<TypeScriptExample>
191+
```
192+
const context = await sandbox.createCodeContext({
193+
language: 'javascript'
194+
});
195+
196+
// Execute async code that fetches data
197+
const result = await sandbox.runCode(`
198+
(async () => {
199+
// Simulate async data fetching
200+
const fetchData = (id) => Promise.resolve({ id, name: 'Item ' + id });
201+
202+
const item1 = await fetchData(1);
203+
const item2 = await fetchData(2);
204+
205+
return [item1, item2];
206+
})()
207+
`, { context: context.id });
208+
209+
console.log('Results:', result.results[0].json);
210+
// Results: [{ id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }]
211+
```
212+
</TypeScriptExample>
213+
214+
**Error handling for async code**:
215+
216+
<TypeScriptExample>
217+
```
218+
const result = await sandbox.runCode(`
219+
(async () => {
220+
throw new Error('Async operation failed');
221+
})()
222+
`, { context: context.id });
223+
224+
if (result.error) {
225+
console.error('Error:', result.error.message);
226+
console.error('Stack:', result.logs.stderr.join('\n'));
227+
}
228+
```
229+
</TypeScriptExample>
230+
186231
## Execute AI-generated code
187232

188233
Run LLM-generated code safely in a sandbox:

0 commit comments

Comments
 (0)