and spawning a thread and using block_on, like below:
pub(crate) fn run_js(args: &RunArgs) { let file_path = args.file.to_owned().into_string().unwrap(); let handle = tokio::runtime::Handle::current(); std::thread::spawn(move || { // Using Handle::block_on to run async code in the new thread. if let Err(error) = handle.block_on(run_js(file_path.as_str())) { eprintln!("error: {error}"); } }) .join() .unwrap();}
pub(crate) fn run_js(args: &RunArgs) { let file_path = args.file.to_owned().into_string().unwrap(); let handle = tokio::runtime::Handle::current(); std::thread::spawn(move || { // Using Handle::block_on to run async code in the new thread. if let Err(error) = handle.block_on(run_js(file_path.as_str())) { eprintln!("error: {error}"); } }) .join() .unwrap();}
But the moment I execute the example script, it fails at
setTimeout
setTimeout
.
console.log("Waiting 2 seconds for timeout...");setTimeout(() => { console.log("Hello World from timeout!");}, 2000);
console.log("Waiting 2 seconds for timeout...");setTimeout(() => { console.log("Hello World from timeout!");}, 2000);
I end up with output like below:
[out]: "Waiting 2 seconds for timeout..."Error: × Main thread panicked. ├─▶ at /Users/my_user_name/.cargo/registry/src/index.crates.io-6f17d22bba15001f/deno_unsync-0.3.4/src/task.rs:56:3 ╰─▶ assertion failed: Handle::current().runtime_flavor() == RuntimeFlavor::CurrentThread help: set the `RUST_BACKTRACE=1` environment variable to display a backtrace.fatal runtime error: failed to initiate panic, error 5
[out]: "Waiting 2 seconds for timeout..."Error: × Main thread panicked. ├─▶ at /Users/my_user_name/.cargo/registry/src/index.crates.io-6f17d22bba15001f/deno_unsync-0.3.4/src/task.rs:56:3 ╰─▶ assertion failed: Handle::current().runtime_flavor() == RuntimeFlavor::CurrentThread help: set the `RUST_BACKTRACE=1` environment variable to display a backtrace.fatal runtime error: failed to initiate panic, error 5