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

Is it possible to call async functions? If yes how? #876

Closed
weezy20 opened this issue Mar 14, 2022 · 3 comments
Closed

Is it possible to call async functions? If yes how? #876

weezy20 opened this issue Mar 14, 2022 · 3 comments

Comments

@weezy20
Copy link

weezy20 commented Mar 14, 2022

I am using a rust crate that requires the use of async functions, and so my main function looks like this:

#[neon::main]
fn main(mut cx: ModuleContext) -> NeonResult<()> {
    cx.export_function("async_fn", async_fn)?;
    Ok(())
}

I would like to .await on the async_fn, what is the way to achieve this?

@kjvalencik
Copy link
Member

There is a proposal to simplify, but it currently requires some boilerplate.

See the tokio-fetch example for more details.

If it's acceptable to block the JavaScript main thread while awaiting, it can be significantly simplified by using block_on to synchronously block until the Future completes.

@weezy20
Copy link
Author

weezy20 commented Mar 14, 2022

For testing, I would like to use block_on for the time being just to make sure it actually works. How do you suggest I write this though?

@kjvalencik
Copy link
Member

I suggest trying the tokio-fetch example. Let me know if you have any questions.

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

2 participants