Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I make an "async/await" operator for Jint? #755

Closed
MauryDev opened this issue Jul 2, 2020 · 9 comments
Closed

How do I make an "async/await" operator for Jint? #755

MauryDev opened this issue Jul 2, 2020 · 9 comments

Comments

@MauryDev
Copy link

MauryDev commented Jul 2, 2020

I made a function in C# for Promise, Async and Await, but I don't want it to be "async / await" functions in javascript, but an operator.

It works in my custom Jint.Engine like that

var async_example = async(function()
{
  return 2;
})
var t = async_example();
console.log(await(t))
// 2

But I would like it that way:

var async_example = async function()
{
  return 2;
}
var t = async_example();
console.log(await t)
// 2

I wanted to add the "async / await" operator to Jint. Please help me.

@MauryDev MauryDev changed the title How do I make an "async" operator for Jint? How do I make an "async/await" operator for Jint? Jul 2, 2020
@danbopes
Copy link

Promises have yet to have been implemented. (So no async/await yet)

@lahma
Copy link
Collaborator

lahma commented Dec 27, 2020

You might want to follow the building block PR, Promises.

@koculu
Copy link

koculu commented Oct 22, 2021

I have created a partial, blocking and simple await support for C# Tasks and ValueTask.
This might be a temporary solution until real await comes in.
https://gist.github.com/koculu/86cf538bcd4411d84e6206cc7fe7758f

@sebastienros Would it be fine if you add this partial support until proper await implementation done?
This can let people to write async scripts with await instead of converting everything to Promises.

@lahma
Copy link
Collaborator

lahma commented Dec 11, 2021

Closing this as duplicate of #514 , we have the promises, but async/await needs to be done. I think generators need to be done as it has some building blocks once again.

@lahma lahma closed this as completed Dec 11, 2021
@lahma
Copy link
Collaborator

lahma commented Oct 20, 2022

async/await has been implemented as first class citizen in main branch.

@sfmskywalker
Copy link

Hi @lahma , does this also mean one could set an C# function that returns a Task that can be awaited in JS? For example:

engine.SetValue("testAsync", (Func<Task<object?>>)(async () => await Task.FromResult(new { foo = "bar" })));

I tried this with the following JS:

const bar = await testAsync();

Which throws a ParserException with "Unexpected identifier".

@lahma
Copy link
Collaborator

lahma commented Oct 11, 2023

Sorry I've been a bit absent as my wife passed away a month ago and I had to prioritize things a bit. I think there's a PR #1567 that tries to fill the gap between .NET Task and JS's async, but there might be dragons.

If you get Unexpected identifier that might signal that you are not in async context as that's something that is being expected by the parser at the moment (inside async function).

@sfmskywalker
Copy link

Sorry for your loss Marko. I noticed you have merged that PR, which is fantastic - thank you! I'll go take a look.

@sfmskywalker
Copy link

I never reported back, but I wanted to let you know that the bridging from Promise to Task and the async/await keywords work super amazing 😍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants