AV6
AV613mo ago

calling a javascript function and waiting for the promise with `rusty_v8`

let result = function.call(scope, recv, &[]).expect("couldnt run");
/*i got this result which is a promise. now how do i get the value of the promise

let value = ??
*/
let result = function.call(scope, recv, &[]).expect("couldnt run");
/*i got this result which is a promise. now how do i get the value of the promise

let value = ??
*/
result.is_promise is true promise state is Pending scope.has_pending_background_tasks() is false which confuses me. Isnt this supposed to be true if there are unresolved promises?
13 Replies
Andreu Botella (they/them)
v8::Isolate::has_pending_background_tasks is about V8-managed asynchronous tasks, like GC or wasm compilation if you're using deno_core or deno_runtime, the promise is managed by the deno_core event loop
Andreu Botella (they/them)
and you could wait until a promise resolves with JsRuntime::resolve_value (https://docs.rs/deno_core/0.189.0/deno_core/struct.JsRuntime.html#method.resolve_value)
JsRuntime in deno_core - Rust
A single execution context of JavaScript. Corresponds roughly to the “Web Worker” concept in the DOM. A JsRuntime is a Future that can be used with an event loop (Tokio, async_std). The JsRuntime future completes when there is an error or when all pending ops have completed.
Andreu Botella (they/them)
if you're not using deno_core, you'd have to delegate to whichever event loop handles the promise
AV6
AV613mo ago
i am using rusty_v8. But i am unsure how to handle the event loop here. is it possible to get the value synchronously? or do you have any examples which i can refer to? i have the result: Local<Value> which is a promise (result.is_promise is true). what i am trying to do is let value = get_inner_value_synchronously(result) . is this possible?
AV6
AV613mo ago
something like an "await" version in rust
Andreu Botella (they/them)
in the JS code you posted in the rusty_v8 issue, it wasn't awaiting on anything, right? in that case, I would have expected v8::Function::call to run microtasks, after which the promise should not have been pending anymore it's odd that it's still pending
AV6
AV613mo ago
it was
async function main() {
return "helloworld"
}
async function main() {
return "helloworld"
}
yea!, thats what is puzzling me. promise.state() is Pending, i tried pumping the loop as well in a while loop
while v8::Platform::pump_message_loop(&v8::V8::get_current_platform(), scope, false) {
// do nothing
}
while v8::Platform::pump_message_loop(&v8::V8::get_current_platform(), scope, false) {
// do nothing
}
Andreu Botella (they/them)
yeah, that will not work
AV6
AV613mo ago
this main function is being called from rust
let result = function.call(scope, recv, &[]).expect("couldnt run");
let result = function.call(scope, recv, &[]).expect("couldnt run");
thats how i got the result promise which i am expecting will resolve to "helloworld" if i make the above js function sync (just removing the async) then the whole thing works, i get the result string "helloworld" out. so the function call works. this result above is giving result.is_promise() true. which i expect it to. But i do not understand why it is always in pending scope.perform_microtask_checkpoint(); i tried running this as well.
Andreu Botella (they/them)
I'm not sure
AV6
AV613mo ago
GitHub
how to synchronously wait for a promise value · Issue #1259 · denol...
Hi, I am trying to call a javascript function from rust using v8 isolates. Something like this. let isolate = &mut v8::Isolate::new(v8::CreateParams::default()); let handle_scope = &mut v8:...
AV6
AV613mo ago
asked here, got the answer! thanks a lot!