#Passing a JS promise to a Rust function
60 messages · Page 1 of 1 (latest)
Is that a chatGPT recommendation? 👀
no?
Jokes aside, that’s not really possible since promises are as a language feature incompatible with anything within rust
where did you find that function?
what about futures
API documentation for the Rust execute_promise fn in crate tauri.
futures are pretty compatible with promises are they not?
futures are inspired by promises yeah, but they work very differently under the hood
yeah i mean from a high level
that’s an internal method from the IPC system that I think we made public by accident
ah right
yeah that’s true
from a high level they are very similar
do you think there is any possibility for translation between the two?
hmm depends on what your goal with it is
you could basically wrap a promise in something that emits an event to the core when it resolves or rejects
right
and then in your command you would have something that implements the future trait and in turn awaits a channel or something
so yeah you probably could, but you also could just await the promise in the frontend and then pass the resolved value to the core
that's what I'm doing at the moment, but there's opportunity to make things cleaner by handling it in the core
i don't know about you but it seems resolving them in the core could be a good feature for tauri?
your still resolving them on the webview thread though
you’re just notifying the core thread once that promises is done
you have the opportunity to resolve them on a separate thread from the core though
wdym?
i mean like
if you're handling the promise in the core, you have the option to spawn a thread to handle it etc
yeah that’s what happens right now anyway when a command gets called
not quite, but simplified anyway
but still, you can’t drive a JS promise from Rust when you mean that
it still has to run and resolve in the context of the JS event loop
true
and once that is done you can send a message to Rust
i suppose then you get into passing the whole context to rust which would just get awful i'm guessing (if you were to resolve it in the core)
mhm
hmm just keep in mind that we don’t have access to the JS engines at all
from rust
so we cannot get anything from it that JS can’t
oh of course
and without access to the JS engine’s state and runtime n things you would need to copy the whole stack and heap
but even then you can’t really
yeah no
oh and somehow get the code that’s involved in driving the future (JS callbacks n stuff) and somehow convert them into something rust can call?
it’s a fun idea though
it doesn't sound feasable now does it
lmfao
in principle it would have been nice lol
nah sadly not
awaiting a FnOnce or something would be so cool though!!
actually GraalVM is kinda interesting here as they actually allow you to interface between a number if languages in this way
it’s basically just a java vm that can also run other languages
that way around is much easier (embedding a statically compiled language within a dynamic one) where embedding a dynamic language in a static one is always annoying
where it belongs hahaha