Skip to content

Commit

Permalink
fix async eval tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rscarson committed Dec 4, 2024
1 parent e0f79d2 commit 31bfc19
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
6 changes: 5 additions & 1 deletion examples/async_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ fn main() -> Result<(), Error> {
"globalThis.sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));",
)?;

// Can be run as blocking
runtime.eval::<u32>("sleep(1000).then(() => 1)")?;

// Or as async
let future = async {
let result: Promise<u32> = runtime.eval("sleep(1000).then(() => 2)")?;
let result: Promise<u32> = runtime.eval_immediate("sleep(1000).then(() => 2)")?;
result.into_future(&mut runtime).await?;

Ok::<(), Error>(())
Expand Down
25 changes: 12 additions & 13 deletions src/inner_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> = runtime
.decode_value(result)
.expect("Could not decode promise");
let result: Promise<u32> = 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(())
Expand Down

0 comments on commit 31bfc19

Please sign in to comment.