---
title: "Run a JavaScript async function body from C#"
url: https://recursiveai.org/oneecma/cookbook/call-javascript-async-function-body/
description: "Run a JavaScript async function body from C# with OneEcma. Use await and return in supplied source, then read the returned value in .NET."
version: "1.1.1"
---

# Run a JavaScript async function body from C#

**OneEcma** is a free, open-source JavaScript interpreter for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneEcma) · [GitHub](https://github.com/recursive-ai-oss/oneecma-csharp) · [Docs](https://recursiveai.org/oneecma/docs/) · [Download example (.zip)](https://recursiveai.org/oneecma/cookbook/call-javascript-async-function-body/call-javascript-async-function-body.zip)

Use `EvaluateAsyncBody` to run `await` and `return` without wrapping the text in a function.

```shell
dotnet add package RecursiveAI.OneEcma
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/oneecma/cookbook/call-javascript-async-function-body/call-javascript-async-function-body.zip), then `dotnet run`.

```text
42
```

The 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`:

```csharp
var promise = engine.EvaluateAsyncBody("""
    "use strict";
    await Promise.resolve(21);
    """, "no-result.js");
var result = await engine.AwaitAsync(promise);
Console.WriteLine(result.IsUndefined);
```

```text
True
```

To catch a JavaScript error in C#, replace the code after `new ScriptEngine()` with a `try` block around both calls:

```csharp
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);
}
```

```text
Error: Cannot finish
```

## Related recipes

- [Call a named JavaScript function from C#](https://recursiveai.org/oneecma/cookbook/call-javascript-function/)
- [Run JavaScript modules in C#](https://recursiveai.org/oneecma/cookbook/run-javascript-modules/)
- [Run JavaScript from C#](https://recursiveai.org/oneecma/cookbook/run-javascript/)

## 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`](https://recursiveai.org/oneecma/docs/host-api/) 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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function), 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.
