async/await and callback support

See original GitHub issue

I was surprised to find no issues nor pull requests to support C#/JS async/await and Task support.

In a perfect world, I could make that transparent and “halt” the calling method (like #192), or use a callback mechanism (which would make scripts much more complex however).

I’d prefer to avoid using return task.Result and friends, so that I don’t get into a deadlock eventually.

Before investigating this further, did anyone actually attempt any async/await support in Jint? @sebastienros is this something you’d be interested in supporting, e.g. through the native ECMA-262 async/await (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)? Any failed or partial attempt?

To avoid any confusion, given this C# code:

public async Task<string> loadSomething(string path) {
  return await File.ReadAllTextAsync(path);
}

I’d consider any of these JavaScript equivalents to be correct (in order of preference):

// Pausing (easiest for script writers):
var x = loadSomething('test.txt');
// Async Function (concise)
var x = await loadSomething('test.txt');
// Promises (well known)
var x;
loadSomething('test.txt').then(function(result) { x = result });;
// Callbacks (worst case)
var x;
loadSomething('test.txt', function(result) { x = result });;

Any ideas or feedback on that is welcome; I’d like to avoid spending any time on this if it would not be merged, or if the effort is too large for what I can afford.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:13
  • Comments:58 (27 by maintainers)

github_iconTop GitHub Comments

15reactions
lahmacommented, May 17, 2021

Promise support has been implemented, but no support for async/await keywords yet.

7reactions
ta10212commented, Aug 16, 2018

Hi everyone, I echo @christianrondeau in the desire for async/await support.

While promises would work, it would be even simpler if the Engine could just “await” a function delegate passed to “SetValue” - if that delegate wrapped an asynchronous method.

Suppose we had the following C# code:

Engine engine = new Engine(cfg => cfg.AllowClr());
Func<string, Task<string>> thisFunc = DoOperation;
engine.SetValue("DoOperation", thisFunc);

//C# method of DoOperation
public async Task<string> DoOperation(string input)
{
   //assume that SomeAsynchronousOperation returns a string...

   return await SomeAsynchronousOperation();
}

The ideal scenario would then be for an “ExecuteAsync” method on the engine like:

engine.ExecuteAsync("DoOperation('my_input');");

…where internally the engine would “await” the function delegate at the time of invocation.

@sebastienros , is this at all possible? If not, are there any other workarounds you might suggest?

Jint is a phenomenal piece of software though in my particular case, I’m dealing with heavy network I/O. As such, async/await support is critical.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript — from callbacks to async/await
JavaScript is synchronous. This means that it will execute your code block by order after hoisting. Before the code executes, var and function...
Read more >
javascript - How to "await" for a callback to return?
async /await is not magic. An async function is a function that can unwrap Promises for you, so you'll need api.on() to return...
Read more >
Using Callbacks With Async/Await - Maxim Orlov
Suppose we have a getUserAddress function that's written using modern async/await. Inside this function, we're calling fetchUser which uses a callback-based ...
Read more >
Flow Control in JavaScript: Callbacks, Promises, async/await
The solution is asynchronous processing. Rather than wait for completion, a process is told to call another function when the result is ready....
Read more >
How to go from Callbacks to Async Await in Node
I recently got a chance to work with Node again. Being a huge fan of promises, I never tried async await. Why? Because...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found