JacobZwangJ
Denoβ€’2y agoβ€’
1 reply
JacobZwang

block js with #[op2] fn without blocking rust thread

I'm using deno_core and I'm looking for a way to block the js main thread without using a sync function to block rust's thread.
#[op2(async)]
#[string]
async fn op_sleep() -> Result<(), AnyError> { // should not block rust thread
    tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
    Ok(())
}

Deno.core.ops.op_sleep() // should block js runtime for 1 second

Basically what should happen is:
1. op_sleep is called from js.
2. rust pauses the js runtime for 1 second. during this 1 second, rust thread is can still be executing other isolates on the same thread.
3. after 1 second ends, rust resumes the js runtime.

Is this possible using deno_core and op2 or do I need to use lower level apis?
Was this page helpful?