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

create a new Promise based on a resolver

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);

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));

get the handle which can be used to resolve a promise

Trait Implementations

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.