Struct quickjs_runtime::esvalue::EsPromise [−][src]
can be used to create a new Promise which is resolved with the resolver function
Example
use quickjs_runtime::esruntimebuilder::EsRuntimeBuilder; use quickjs_runtime::esvalue::{EsPromise, EsValueConvertible}; use std::time::Duration; use quickjs_runtime::esscript::EsScript; use log::LevelFilter; let rt = EsRuntimeBuilder::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(EsScript::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
impl EsPromise
[src]
pub fn new<R>(resolver: R) -> Self where
R: FnOnce() -> Result<EsValueFacade, String> + Send + 'static,
[src]
R: FnOnce() -> Result<EsValueFacade, String> + Send + 'static,
pub fn new_unresolving() -> Self
[src]
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::esruntimebuilder::EsRuntimeBuilder; use quickjs_runtime::esscript::EsScript; use std::time::Duration; use quickjs_runtime::esvalue::{EsPromise, EsValueConvertible}; let rt = EsRuntimeBuilder::new().build(); // prep a function which reacts to a promise rt.eval_sync(EsScript::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", 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));
pub fn get_handle(&self) -> Arc<EsPromiseResolvableHandle>
[src]
get the handle which can be used to resolve a promise
Trait Implementations
impl EsValueConvertible for EsPromise
[src]
fn as_js_value(&mut self, q_ctx: &QuickJsContext) -> Result<JSValueRef, EsError>
[src]
fn to_es_value_facade(self) -> EsValueFacade where
Self: Sized + Send + 'static,
[src]
Self: Sized + Send + 'static,
fn is_null(&self) -> bool
[src]
fn is_undefined(&self) -> bool
[src]
fn is_bool(&self) -> bool
[src]
fn get_bool(&self) -> bool
[src]
fn is_str(&self) -> bool
[src]
fn get_str(&self) -> &str
[src]
fn is_i32(&self) -> bool
[src]
fn get_i32(&self) -> i32
[src]
fn is_f64(&self) -> bool
[src]
fn get_f64(&self) -> f64
[src]
fn is_function(&self) -> bool
[src]
fn invoke_function_sync(
&self,
_args: Vec<EsValueFacade>
) -> Result<EsValueFacade, EsError>
[src]
&self,
_args: Vec<EsValueFacade>
) -> Result<EsValueFacade, EsError>
fn invoke_function(&self, _args: Vec<EsValueFacade>) -> Result<(), EsError>
[src]
fn invoke_function_batch_sync(
&self,
_args: Vec<Vec<EsValueFacade>>
) -> Vec<Result<EsValueFacade, EsError>>
[src]
&self,
_args: Vec<Vec<EsValueFacade>>
) -> Vec<Result<EsValueFacade, EsError>>
fn invoke_function_batch(
&self,
_args: Vec<Vec<EsValueFacade>>
) -> Result<(), EsError>
[src]
&self,
_args: Vec<Vec<EsValueFacade>>
) -> Result<(), EsError>
fn is_promise(&self) -> bool
[src]
fn await_promise_blocking(
&self,
_timeout: Duration
) -> Result<Result<EsValueFacade, EsValueFacade>, RecvTimeoutError>
[src]
&self,
_timeout: Duration
) -> Result<Result<EsValueFacade, EsValueFacade>, RecvTimeoutError>
fn add_promise_reactions(
&self,
_then: PromiseReactionType,
_catch: PromiseReactionType,
_finally: Option<Box<dyn Fn() + Send + 'static>>
) -> Result<(), EsError>
[src]
&self,
_then: PromiseReactionType,
_catch: PromiseReactionType,
_finally: Option<Box<dyn Fn() + Send + 'static>>
) -> Result<(), EsError>
fn is_object(&self) -> bool
[src]
fn get_object(&self) -> &HashMap<String, EsValueFacade>
[src]
fn get_object_mut(&mut self) -> &mut HashMap<String, EsValueFacade>
[src]
fn is_array(&self) -> bool
[src]
fn get_array(&self) -> &Vec<EsValueFacade>
[src]
fn get_array_mut(&mut self) -> &mut Vec<EsValueFacade>
[src]
Auto Trait Implementations
impl RefUnwindSafe for EsPromise
[src]
impl Send for EsPromise
[src]
impl Sync for EsPromise
[src]
impl Unpin for EsPromise
[src]
impl UnwindSafe for EsPromise
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Pointable for T
pub const ALIGN: usize
type Init = T
The type for initializers.
pub unsafe fn init(init: <T as Pointable>::Init) -> usize
pub unsafe fn deref<'a>(ptr: usize) -> &'a T
pub unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T
pub unsafe fn drop(ptr: usize)
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,