-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Callbacks on next tick are not called #16
Comments
Yeah I noticed this limitation. The problem is that the returned value is just a promise and the runtime will have pending jobs. |
Probably worth asking for support in the QuickJS mailing list. |
It is implemented in new Promise(resolve => resolve('hi')).then(msg => console.log(msg)) works in the console. I'm trying to understand how is it done there. |
So there is a function js_std_loop, a comment says that it implements the main loop. An additional method can be added to the |
Yeah I saw that function but that doesn't really give us what we need.
I'm a bit busy the next few days, but I'd accept a PR. function __resolvePromise(p) {
globalThis.__promiseResult = null;
p
.then(value => { globalThis.__promiseResult = value; })
.catch(e => throw e);
} |
I understood it that use quick_js::{Context, JsValue};
fn main() {
let context = Context::new().unwrap();
context
.add_callback("print_cb", |s: String| {
println!("value: {}", s);
JsValue::Null
})
.unwrap();
context
.eval("new Promise(resolve => resolve('hi')).then(print_cb); null")
.unwrap();
context.
.std_loop()
.unwrap();
} The thread would be blocked by
Yeah, it should work. But as I see it, the loop that would check the global variable probably still might be better to run in a separate thread in order to not do the looping in the main thread. |
I wouldn't want to make any threading decisions for the user. We wouldn't use the Actuall it seems like |
As to your point with |
Do you mean converting JS promises to Rust futures? So that it could be used like this: let future1: Future<String> = ctx.eval("new Promise(resolve => resolve('hello 1'))").unwrap();
let future2: Future<String> = ctx.eval("new Promise(resolve => resolve('hello 2'))").unwrap(); and then each call of |
We would probably need a And for this case, we would definitely need a worker thread that runs the loop, keeps track of the futures and resolves them when they are ready. But the first step for me would be to make the blocking promise handling work. |
Alright, so basic If Note: currently, I'll think about how to make this happen. Example: #[test]
fn eval_async() {
let c = Context::new().unwrap();
let value = c
.eval(
r#"
new Promise((resolve, _) => {
resolve(33);
})
"#,
)
.unwrap();
assert_eq!(value, JsValue::Int(33));
let res = c.eval(
r#"
new Promise((_resolve, reject) => {
reject("Failed...");
})
"#,
);
assert_eq!(
res,
Err(ExecutionError::Exception(JsValue::String(
"Failed...".into()
)))
);
} |
@a-rodin you can now basically implement your |
@theduke I was thinking about it and now I'm a bit concerned about making the loop opaque for the user. The main concern is that it makes it impossible to use in cases when some other event loop is also running, for example the one from |
I'd like to keep |
My main use case was to use fetch API from QuickJS. It is not implemented in QuickJS, but can be implemented for example by a third-party crate. The call to |
Using this feature in https://github.com/galvez/fast-vue-ssr/ (now a fully featured Vue.js app up and running!) I noticed however |
This code prints the message:
However, this code doesn't:
It is not the case that the program doesn't wait until the callback is called, as adding for example
at the end of
main
still doesn't output the message.The text was updated successfully, but these errors were encountered: