GithubHelp home page GithubHelp logo

Comments (4)

andrieshiemstra avatar andrieshiemstra commented on June 21, 2024

Well, i'll look into the code but my first guess is that the runtime is allready dropped when the async code runs.. just for the test, try to add a thread::sleep(Duration::from_secs(1)) after your last .await; in main()

from quickjs_es_runtime.

andrieshiemstra avatar andrieshiemstra commented on June 21, 2024

yeah, try something like this.... return promise from qjs and await it before exiting async main

use quickjs_runtime::builder::QuickJsRuntimeBuilder;
use quickjs_runtime::jsutils::{JsError, Script};
use quickjs_runtime::values::JsValueFacade;
use serde_json::Value;

async fn cat_index() -> Result<Value, JsError> {
    let value = serde_json::from_str("{\"abc\": \"i'm a obj made rusty\"}").unwrap();
    println!("cat_index running");
    Ok(value)
}

#[tokio::main]
async fn main() {
    println!("Hello, world!");

    let rt = QuickJsRuntimeBuilder::new().build();

    rt.loop_realm(None, |_rt, realm| {
        realm
            .install_function(
                &[],
                "cat_elastic",
                |_rt, realm, _this, _args| {
                    realm.create_resolving_promise_async(cat_index(), |realm, value| {
                        realm.serde_value_to_value_adapter(value)
                    })
                },
                0,
            )
            .unwrap();

        realm
            .install_function(
                &[],
                "log",
                |_rt, realm, _this, args| {
                    println!("{:?}", args[0].to_string());
                    realm.create_undefined()
                },
                1,
            )
            .unwrap();
    })
    .await;

    let promise_jsvf = rt
        .eval(
            None,
            Script::new(
                "promiseMeThis.js",
                r#"
        async function myCode(){
            let res = await cat_elastic();
            return res;
        };
        // eval statement, thus returning the promise
        myCode()
    "#,
            ),
        )
        .await
        .expect("Script failed misserably");

    if let JsValueFacade::JsPromise { cached_promise } = promise_jsvf {
        // of boy oh boy we got a promise
        let resolved_value = cached_promise
            .get_promise_result()
            .await
            .expect("Promise timed out?");

        match resolved_value {
            Ok(prom_resolved) => {
                println!(
                    "Promise resolved to {:?}",
                    prom_resolved
                        .to_json_string()
                        .await
                        .expect("no not so stringable?")
                );
            }
            Err(_prom_rejected) => {
                panic!("Promise was rejected");
            }
        }
    }
}

from quickjs_es_runtime.

andrieshiemstra avatar andrieshiemstra commented on June 21, 2024

Ps just for clarity, this doesn't mean you always have to .await promises for them to run or anything, the problem in this case was just that the runtime would be destroyed before the async code ran...

from quickjs_es_runtime.

FlondorDev avatar FlondorDev commented on June 21, 2024

ok, this last piece of code solved my issue

if let JsValueFacade::JsPromise { cached_promise } = promise_jsvf {
        // of boy oh boy we got a promise
        let resolved_value = cached_promise
            .get_promise_result()
            .await
            .expect("Promise timed out?");

        match resolved_value {
            Ok(prom_resolved) => {
                println!(
                    "Promise resolved to {:?}",
                    prom_resolved
                        .to_json_string()
                        .await
                        .expect("no not so stringable?")
                );
            }
            Err(_prom_rejected) => {
                panic!("Promise was rejected");
            }
        }
    }

basically yeah the code execution dropped before promise completion, i searched for a while a way to wait for the promise to end but i found the docs a bit lacking

from quickjs_es_runtime.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.