#Passing a JS promise to a Rust function

60 messages · Page 1 of 1 (latest)

empty acorn
#

Hi, how do you pass a Promise from JavaScript to Rust through invoke, execute the promise in Rust and work with the result? I understand tauri::execute_promise exists but I can not find usage examples. Many thanks.

compact zinc
#

Is that a chatGPT recommendation? 👀

empty acorn
#

no?

compact zinc
#

Jokes aside, that’s not really possible since promises are as a language feature incompatible with anything within rust

compact zinc
empty acorn
#

what about futures

empty acorn
#

futures are pretty compatible with promises are they not?

compact zinc
empty acorn
#

yeah i mean from a high level

compact zinc
empty acorn
#

ah right

compact zinc
#

from a high level they are very similar

empty acorn
#

do you think there is any possibility for translation between the two?

compact zinc
#

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

empty acorn
#

right

compact zinc
#

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

empty acorn
#

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?

compact zinc
#

your still resolving them on the webview thread though

#

you’re just notifying the core thread once that promises is done

empty acorn
#

you have the opportunity to resolve them on a separate thread from the core though

compact zinc
#

wdym?

empty acorn
#

if you're handling the promise in the core, you have the option to spawn a thread to handle it etc

compact zinc
#

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

empty acorn
#

true

compact zinc
#

and once that is done you can send a message to Rust

empty acorn
#

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)

compact zinc
#

from rust

#

so we cannot get anything from it that JS can’t

empty acorn
#

oh of course

compact zinc
#

but even then you can’t really

empty acorn
#

yeah no

compact zinc
#

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

empty acorn
#

it doesn't sound feasable now does it

#

lmfao

#

in principle it would have been nice lol

compact zinc
empty acorn
#

awaiting a FnOnce or something would be so cool though!!

compact zinc
#

actually GraalVM is kinda interesting here as they actually allow you to interface between a number if languages in this way

empty acorn
#

oh interesting

#

i haven't seen that before

compact zinc
#

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

empty acorn
#

i can imagine yeah

#

javascript on the jvm is cursed though

compact zinc
#

where it belongs hahaha

empty acorn
#

oml

#

anyway thanks for your help!