How do I use Rust code in Node.js such that the Rust function accepts a JavaScript function as a callback?

Viewed 68

It would look something like this in Rust; handler would come from JavaScript:

let url = "127.0.0.1:4445"; // JavaScript url
let address = SocketAddr::from_str(url).await.unwrap();
let listener = Listener::bind(&address).await.unwrap();
let mut result = false;
while let Some(stream) = listener.incoming().next().await {
    handler(conn); // JavaScript handler
}

I'm planning to use FFI for the integration in Node.js, but I'm not sure how to use the JavaScript function as a callback from Rust.

0 Answers
Related