async/await and callback support
See original GitHub issueI 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:
- Created 5 years ago
- Reactions:13
- Comments:58 (27 by maintainers)
Promise support has been implemented, but no support for async/await keywords yet.
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:
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.