Skip to content

Commit 740309f

Browse files
Expand Promise handling examples with comprehensive patterns
Add examples for async IIFEs, Promise.resolve with objects, and nested async operations to demonstrate the full range of Promise patterns supported in JavaScript/TypeScript code execution contexts. These patterns are verified by the new tests in PR #246 and address issue #206 where Promise-based code previously returned empty objects instead of resolved values. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 62d28c8 commit 740309f

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,40 @@ When executing JavaScript or TypeScript code, if the last expression evaluates t
9898
```
9999
const ctx = await sandbox.createCodeContext({ language: 'javascript' });
100100
101-
const result = await sandbox.runCode(`
101+
// Async function
102+
const result1 = await sandbox.runCode(`
102103
async function fetchData() {
103104
return { status: 'success', value: 42 };
104105
}
105106
fetchData()
106107
`, { context: ctx });
107-
108-
console.log(result.results[0].json); // { status: 'success', value: 42 }
108+
console.log(result1.results[0].json); // { status: 'success', value: 42 }
109+
110+
// Async IIFE
111+
const result2 = await sandbox.runCode(`
112+
(async () => {
113+
const value = await Promise.resolve(10);
114+
return value * 2;
115+
})()
116+
`, { context: ctx });
117+
console.log(result2.results[0].json); // 20
118+
119+
// Promise.resolve with objects
120+
const result3 = await sandbox.runCode(
121+
'Promise.resolve({ status: "success", data: [1, 2, 3] })',
122+
{ context: ctx }
123+
);
124+
console.log(result3.results[0].json); // { status: "success", data: [1, 2, 3] }
125+
126+
// Nested async operations
127+
const result4 = await sandbox.runCode(`
128+
(async () => {
129+
const a = await Promise.resolve(10);
130+
const b = await Promise.resolve(20);
131+
return a + b;
132+
})()
133+
`, { context: ctx });
134+
console.log(result4.results[0].json); // 30
109135
```
110136
</TypeScriptExample>
111137

0 commit comments

Comments
 (0)