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

Can I await javascript promise from rust? #37

Open
kdy1 opened this issue Jun 8, 2020 · 5 comments
Open

Can I await javascript promise from rust? #37

kdy1 opened this issue Jun 8, 2020 · 5 comments
Labels
async enhancement New feature or request

Comments

@kdy1
Copy link

kdy1 commented Jun 8, 2020

Hi. I'm the author of swc and I'm considering switching from neon to node-bindgen.

Can I store js function on struct and call it from other threads?
Neon supports it via EventHandler.

Also, I need to wait for js promises to be resolved from rust code. Is it possible?

If these features are on the roadmap, please let me know.

@sehz
Copy link
Collaborator

sehz commented Jun 8, 2020

Just aware that node-bindgen uses Rust's async infrastructure rather than node's libuv. It uses [async.rs[(https://async.rs) for executor. So far, node-bindgen atomically maps rust's async to promise. So it should be possible to support invoking node.js function or object in the async fn or closure. For example:

/// this is rust function which will invoke js function sum
async fn sum(first: i32,second: i32,js_obj: impl MyJsObj) -> i32 {
    js_obj.sum(first, second).await
}

/// define interface to Js class MyJsObj which implements sum
///
///       export class MyJsObj {
///              constructor() {}
///
///              sum (first, second) {
///                    return first + second;
///              }
//.      }
///
#[nodebingen(js_obj)]
trait MyJsObj {
   async fn sum(first: i32, second: i32)-> i32;     // this is async so it can called from rust's async
}

/// my obj can be stored and manipulated
struct SomeStruct<M: MyObj> {
     my_obj<M>
}

I can put this under roadmap. What is your timetable?

@sehz sehz added async enhancement New feature or request labels Jun 8, 2020
@kdy1
Copy link
Author

kdy1 commented Jun 9, 2020

@sehz
Great! I would really love to see it.

I can put this under roadmap. What is your timetable?

I don't have any strict time table.
However, asspack, a bundler for the swc project, is almost done and as I don't have many things left to do, the sonner is the better for me

@sehz
Copy link
Collaborator

sehz commented Jun 9, 2020

Ok, I will try you put together first phase of solution so can unblock you. Probably in next 1-2 week.

Also, there might be interesting colloberation between two projects. For example, it could be interesting to call JS using TS notation. And ability to export TS binding from Rust code.

@kdy1
Copy link
Author

kdy1 commented Jun 10, 2020

@sehz

Ok, I will try you put together first phase of solution so can unblock you. Probably in next 1-2 week.

Thank you so much! I have a final exam next week so I love the plan.

Also, there might be interesting colloberation between two projects. For example, it could be interesting to call JS using TS notation. And ability to export TS binding from Rust code.

Interesting. Auto-generating .d.ts file will be great. Note that swc_ecma_parser, swc_ecma_ast and swc_ecma_codegen works on stable rust.

@Ryanmtate
Copy link
Contributor

Ryanmtate commented Aug 12, 2020

It looks like this might be achievable using https://nodejs.org/api/n-api.html#n_api_napi_create_async_work

feature/async branch has a work in progress on implementation. Not sure when the work will be completed, but wanted to post this note for later use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
async enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants