diff --git a/examples/async_eval.rs b/examples/async_eval.rs index 452a145..616ea4b 100644 --- a/examples/async_eval.rs +++ b/examples/async_eval.rs @@ -16,8 +16,12 @@ fn main() -> Result<(), Error> { "globalThis.sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));", )?; + // Can be run as blocking + runtime.eval::("sleep(1000).then(() => 1)")?; + + // Or as async let future = async { - let result: Promise = runtime.eval("sleep(1000).then(() => 2)")?; + let result: Promise = runtime.eval_immediate("sleep(1000).then(() => 2)")?; result.into_future(&mut runtime).await?; Ok::<(), Error>(()) diff --git a/src/inner_runtime.rs b/src/inner_runtime.rs index 0a49d9e..52b43af 100644 --- a/src/inner_runtime.rs +++ b/src/inner_runtime.rs @@ -957,21 +957,20 @@ mod test_inner_runtime { let v = runtime.eval("2 + 2").expect("failed to eval"); assert_v8!(v, 4, usize, runtime); + let result = runtime + .eval( + " + let sleep = (ms) => new Promise((r) => setTimeout(r, ms)); + sleep(500).then(() => 2); + ", + ) + .expect("failed to eval"); - run_async_task(|| async move { - let result = runtime - .eval( - " - let sleep = (ms) => new Promise((r) => setTimeout(r, ms)); - sleep(500).then(() => 2); - ", - ) - .expect("failed to eval"); - - let result: Promise = runtime - .decode_value(result) - .expect("Could not decode promise"); + let result: Promise = runtime + .decode_value(result) + .expect("Could not decode promise"); + run_async_task(|| async move { let result: u32 = result.resolve(runtime.deno_runtime()).await?; assert_eq!(result, 2); Ok(())