Use EvaluateAsyncBody to run await and return without wrapping the text in a function.
dotnet add package RecursiveAI.OneEcmaRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneEcma; var engine = new ScriptEngine(); var promise = engine.EvaluateAsyncBody(""" const value = await Promise.resolve(21); return value * 2; """, "double.js"); var result = await engine.AwaitAsync(promise); Console.WriteLine(result.AsNumber);
Unzip the example, then dotnet run.
42The JavaScript is the string in the snippet; double.js is a name for diagnostics, not a file the program opens. If the JavaScript body throws, OneEcma throws a JsException from EvaluateAsyncBody or AwaitAsync.
Variations
Replace the code after new ScriptEngine() with this version to run a strict-mode body that doesn’t return a value. The fulfilled result is JavaScript undefined:
var promise = engine.EvaluateAsyncBody(""" "use strict"; await Promise.resolve(21); """, "no-result.js"); var result = await engine.AwaitAsync(promise); Console.WriteLine(result.IsUndefined);
TrueTo catch a JavaScript error in C#, replace the code after new ScriptEngine() with a try block around both calls:
try { var promise = engine.EvaluateAsyncBody(""" await Promise.resolve(); throw new Error("Cannot finish"); """, "fail.js"); await engine.AwaitAsync(promise); } catch (JsException error) { Console.WriteLine(error.Message); }
Error: Cannot finishRelated recipes
The short answer
Use ScriptEngine.EvaluateAsyncBody(body, fileName) when you have the text of a JavaScript async function body rather than a named function. It returns a JavaScript promise. Pass that promise to await engine.AwaitAsync(promise) to get the JavaScript value from the body’s return. In this example the value is a number, so C# reads AsNumber.
How do I call JavaScript from C# if I only have a function body?
For a named function already defined in the engine, you can look it up and call it. Here you have only the text between the braces. EvaluateAsyncBody interprets that text as the body of an anonymous async function with no parameters. You don’t have to prepend a declaration, append a call, or calculate a line-number offset to make error reports match the original text.
Each evaluation gets its own local declarations. The const value in the snippet isn’t available to a later evaluation; running the body again can declare it again. That doesn’t mean the engine starts from scratch. Free names are resolved against globals, and in non-strict code an assignment to an undeclared name can still create a global. For a body that should reject those accidental assignments, put "use strict"; first, as in the variation. Module import and export declarations don’t belong in this body; use the module API for source that contains them.
Does a JavaScript async function return a promise or a value?
It returns a promise, even if the statement says return 42. In the recipe there are two different waits. JavaScript first awaits Promise.resolve(21) inside the body. C# then awaits OneEcma’s AwaitAsync, which yields the value of the body’s completion. There’s no need to poll the promise or to add a JavaScript .then() just to read the value in C#.
If the body reaches the end without a return, the promise fulfills with undefined, not null or zero. If the body throws, the error is a rejection, not an ordinary returned value. With OneEcma’s default unhandled-rejection setting, the exception can surface in EvaluateAsyncBody itself, so put both the evaluation and the await inside the error handler if you need to catch it. When a body waits on a .NET task exposed by the host, the promise can still be pending when EvaluateAsyncBody returns; AwaitAsync is the same API to wait for that result.