Struct quickjs_runtime::esvalue::EsPromise[][src]

pub struct EsPromise { /* fields omitted */ }

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]

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]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Pointable for T

type Init = T

The type for initializers.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,