Struct quickjs_runtime::esvalue::EsPromise
source · [−]pub struct EsPromise { /* private fields */ }
Expand description
can be used to create a new Promise which is resolved with the resolver function
Example
use quickjs_runtime::builder::QuickJsRuntimeBuilder;
use quickjs_runtime::esvalue::{EsPromise, EsValueConvertible};
use std::time::Duration;
use hirofa_utils::js_utils::Script;
use log::LevelFilter;
let rt = QuickJsRuntimeBuilder::new().build();
rt.set_function(vec!["my", "comp"], "create_prom", |_q_ctx, _args| {
Ok(EsPromise::new(|| {
std::thread::sleep(Duration::from_secs(1));
Ok(9463.to_es_value_facade())
}).to_es_value_facade())
});
rt.eval_sync(Script::new("test_prom.es", "let p765 = my.comp.create_prom(); p765.then((p_res) => {console.log('got ' + p_res)});")).ok().expect("script failed");
std::thread::sleep(Duration::from_secs(2));
Implementations
sourceimpl EsPromise
impl EsPromise
sourcepub fn new<R>(resolver: R) -> Self where
R: FnOnce() -> Result<EsValueFacade, String> + Send + 'static,
pub fn new<R>(resolver: R) -> Self where
R: FnOnce() -> Result<EsValueFacade, String> + Send + 'static,
create a new Promise based on a resolver
sourcepub fn new_async<R>(resolver: R) -> Self where
R: Future<Output = Result<EsValueFacade, String>> + Send + 'static,
pub fn new_async<R>(resolver: R) -> Self where
R: Future<Output = Result<EsValueFacade, String>> + Send + 'static,
create a new Promise based on an async resolver this can be used to implement a resolver which in turn used .await to get results of other async functions
Example
use quickjs_runtime::builder::QuickJsRuntimeBuilder;
use quickjs_runtime::esvalue::{EsPromise, EsValueConvertible};
use hirofa_utils::js_utils::Script;
use std::time::Duration;
async fn a(i: i32) -> i32 {
std::thread::sleep(Duration::from_secs(2));
i * 3
}
let rt = QuickJsRuntimeBuilder::new().build();
rt.set_function(vec!["com", "my"], "testasyncfunc", |_q_ctx, args| {
let input = args[0].get_i32();
let prom = EsPromise::new_async(async move {
let i = a(input).await;
Ok(i.to_es_value_facade())
});
Ok(prom.to_es_value_facade())
})
.ok()
.expect("setfunction failed");
let res_prom = rt
.eval_sync(Script::new("testasync2.es", "(com.my.testasyncfunc(7))"))
.ok()
.expect("script failed");
let res_i32 = res_prom.get_promise_result_sync().expect("prom failed");
assert_eq!(res_i32.get_i32(), 21);
sourcepub fn new_unresolving() -> Self
pub fn new_unresolving() -> Self
create a new Promise which will be resolved later this achieved by creating a Handle which is wrapped in an Arc and thus may be passed to another thread
Example
use quickjs_runtime::builder::QuickJsRuntimeBuilder;
use hirofa_utils::js_utils::Script;
use std::time::Duration;
use quickjs_runtime::esvalue::{EsPromise, EsValueConvertible};
let rt = QuickJsRuntimeBuilder::new().build();
// prep a function which reacts to a promise
rt.eval_sync(Script::new("new_unresolving.es", "this.new_unresolving = function(prom){prom.then((res) => {console.log('promise resolved to %s', res);});};")).ok().expect("script failed");
// prep a EsPromise object
let prom = EsPromise::new_unresolving();
// get the handle
let prom_handle = prom.get_handle();
// call the function with the promise as arg
rt.call_function(vec![], "new_unresolving".to_string(), vec![prom.to_es_value_facade()]);
// start a new thread which resolves the handler after x seconds
std::thread::spawn(move || {
std::thread::sleep(Duration::from_secs(3));
prom_handle.resolve("hello there".to_string().to_es_value_facade());
});
// wait a few secs to see the log output
std::thread::sleep(Duration::from_secs(5));
sourcepub fn get_handle(&self) -> Arc<EsPromiseResolvableHandle>
pub fn get_handle(&self) -> Arc<EsPromiseResolvableHandle>
get the handle which can be used to resolve a promise
Trait Implementations
sourceimpl EsValueConvertible for EsPromise
impl EsValueConvertible for EsPromise
fn as_js_value(
&mut self,
q_ctx: &QuickJsRealmAdapter
) -> Result<JSValueRef, JsError>
fn to_es_value_facade(self) -> EsValueFacade where
Self: Sized + Send + 'static,
fn is_null(&self) -> bool
fn is_undefined(&self) -> bool
fn is_bool(&self) -> bool
fn get_bool(&self) -> bool
fn is_str(&self) -> bool
fn get_str(&self) -> &str
fn is_i32(&self) -> bool
fn get_i32(&self) -> i32
fn is_f64(&self) -> bool
fn get_f64(&self) -> f64
fn is_function(&self) -> bool
fn invoke_function_sync(
&self,
_args: Vec<EsValueFacade>
) -> Result<EsValueFacade, JsError>
fn invoke_function(
&self,
_args: Vec<EsValueFacade>
) -> EsValueFacadeFuture<EsValueFacade, JsError>
fn invoke_function_batch_sync(
&self,
_args: Vec<Vec<EsValueFacade>>
) -> Vec<Result<EsValueFacade, JsError>>
fn invoke_function_batch(
&self,
_args: Vec<Vec<EsValueFacade>>
) -> Result<(), JsError>
fn is_promise(&self) -> bool
fn get_promise_result_sync(&self) -> Result<EsValueFacade, EsValueFacade>
fn get_promise_result(
&self
) -> EsValueFacadeFuture<EsValueFacade, EsValueFacade>
fn add_promise_reactions(
&self,
_then: PromiseReactionType,
_catch: PromiseReactionType,
_finally: Option<Box<dyn Fn() + Send + 'static>>
) -> Result<(), JsError>
fn is_object(&self) -> bool
fn get_object(&self) -> Result<HashMap<String, EsValueFacade>, JsError>
fn is_array(&self) -> bool
fn get_array(&self) -> Result<Vec<EsValueFacade>, JsError>
fn supports_stringify(&self) -> bool
fn stringify(&self) -> Result<String, JsError>
fn is_error(&self) -> bool
fn get_error(&self) -> JsError
Auto Trait Implementations
impl RefUnwindSafe for EsPromise
impl Send for EsPromise
impl Sync for EsPromise
impl Unpin for EsPromise
impl UnwindSafe for EsPromise
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more