deno_core: Asynchronous cannot obtain return value
let script_with_params = "(async () => {12})()";
// execute_script_static
let result = runtime.execute_script_static("<user-script>", &script_with_params)?;
// run_event_loop
runtime.run_event_loop(PollEventLoopOptions::default()).await?;
let scope = &mut runtime.handle_scope();
let local = v8::Local::new(scope, result);
// Deserialize a `v8` object into a Rust type using `serde_v8`,
// in this case deserialize to a JSON `Value`.
let deserialized_value =
serde_v8::from_v8::<serde_json::Value>(scope, local);
Ok(deserialized_value.unwrap().to_string())
result: {}
Excuse me, how should I write it to correctly obtain the return value within asynchrony?
About this issue
- Original URL
- State: closed
- Created 6 months ago
- Comments: 18 (10 by maintainers)
Hello,
Thank you for your continued support. I wanted to update you on my progress and also seek your opinion on an approach I’ve tried.
I managed to execute the script and retrieve the value using the following method:
This method seems to work for executing the script and retrieving the value. However, I am wondering if there is an even better or more efficient way to achieve this. Particularly, I’m interested in understanding if there’s a way to optimize the handling of promises and the extraction of their results.
Any further insights or suggestions you could provide would be greatly appreciated.
Thank you once again for your assistance.
That’s fine, but then I think docstring on
JsRuntime::resolve
should hint that the event loop needs to be polled, from what it reads right now:it hints that it automatically runs the event loop.