Skip to content

Getting the result of promises #3183

Closed Answered by HalidOdat
mlafeldt asked this question in Q&A
Discussion options

You must be logged in to vote

We can use the JsPromise::from_object() static method for this after extracting the object from the JsValue result, then use the JsPromise::state() method to get the state of the promise enum PromiseState

The JsPromise::from_object() doc, has a good example :)

Adjusted to the given code, it would look something like this:

let code = r#"
    (async () => {
        throw new Error("boom")
    })()
"#;

let mut ctx = Context::default();

match ctx.eval(Source::from_bytes(code)) {
    Ok(res) => {
        ctx.run_jobs();
        let promise = res.as_object().cloned().unwrap();
        
        let promise = JsPromise::from_object(promise)?;
        
        assert!(promise.state()?.as_rejected()

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@mlafeldt
Comment options

@HalidOdat
Comment options

@mlafeldt
Comment options

Answer selected by mlafeldt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants