From 0f764b6f63a71c9b0782dc4e8e781dec6425556b Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 25 Nov 2025 20:36:21 +0000 Subject: [PATCH 1/2] Document async/await and Promise handling in Code Interpreter 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 --- src/content/docs/sandbox/api/interpreter.mdx | 54 ++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/content/docs/sandbox/api/interpreter.mdx b/src/content/docs/sandbox/api/interpreter.mdx index 562effd32142ed2..8215977422b87c5 100644 --- a/src/content/docs/sandbox/api/interpreter.mdx +++ b/src/content/docs/sandbox/api/interpreter.mdx @@ -90,6 +90,45 @@ console.log(result.results[0].text); // "15" ::: +**Async/await and Promise handling (JavaScript and TypeScript)**: + +The Code Interpreter automatically awaits Promise results in JavaScript and TypeScript contexts, allowing you to work with async code naturally. + + +``` +const ctx = await sandbox.createCodeContext({ language: 'javascript' }); + +// Async IIFE +const result1 = await sandbox.runCode(` +(async () => { + const value = await Promise.resolve(42); + return { result: value * 2 }; +})() +`, { context: ctx }); + +console.log(result1.results[0].json); // { result: 84 } + +// Direct Promise.resolve +const result2 = await sandbox.runCode( + 'Promise.resolve({ status: "success" })', + { context: ctx } +); + +console.log(result2.results[0].json); // { status: "success" } + +// Nested async operations +const result3 = await sandbox.runCode(` +(async () => { + const a = await Promise.resolve(10); + const b = await Promise.resolve(20); + return { sum: a + b }; +})() +`, { context: ctx }); + +console.log(result3.results[0].json); // { sum: 30 } +``` + + **Error handling**: @@ -104,6 +143,21 @@ if (result.error) { ``` +**Async error handling**: + + +``` +const result = await sandbox.runCode( + 'Promise.reject(new Error("Async error"))', + { language: 'javascript' } +); + +if (result.error) { + console.error(result.error.message); // "Async error" +} +``` + + ### `listCodeContexts()` List all active code execution contexts. From 68c4a9aadc3dc37509e02254de7c9e7d5b22edde Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 25 Nov 2025 20:37:40 +0000 Subject: [PATCH 2/2] Add async/await examples to code execution guide Adds practical examples of async JavaScript/TypeScript execution to the how-to guide, demonstrating data processing workflows with Promises. References cloudflare/sandbox-sdk#246 --- .../docs/sandbox/guides/code-execution.mdx | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/content/docs/sandbox/guides/code-execution.mdx b/src/content/docs/sandbox/guides/code-execution.mdx index f598aa2ad3da50b..622c5ab45add8f7 100644 --- a/src/content/docs/sandbox/guides/code-execution.mdx +++ b/src/content/docs/sandbox/guides/code-execution.mdx @@ -107,6 +107,37 @@ console.log(result.output); // "Mean: 3.0" Context state is lost if the container restarts due to inactivity. For critical data, store results outside the sandbox or design your code to reinitialize as needed. ::: +### Async JavaScript and TypeScript + +The Code Interpreter automatically awaits Promise results in JavaScript and TypeScript contexts: + + +``` +const context = await sandbox.createCodeContext({ + language: 'javascript' +}); + +// Async functions are automatically awaited +const result1 = await sandbox.runCode(` +(async () => { + const response = await Promise.resolve({ data: [1, 2, 3, 4, 5] }); + const sum = response.data.reduce((a, b) => a + b, 0); + return { sum, count: response.data.length }; +})() +`, { context: context.id }); + +console.log(result1.results[0].json); // { sum: 15, count: 5 } + +// Promise.resolve works directly +const result2 = await sandbox.runCode( + 'Promise.resolve({ status: "complete", timestamp: Date.now() })', + { context: context.id } +); + +console.log(result2.results[0].json); // { status: "complete", timestamp: ... } +``` + + ## Handle rich outputs The code interpreter returns multiple output formats: