-
Hey, how can I figure out that the following code throws an exception? 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();
println!("Result: {res:?}"); // Result: Object((Promise) 0x153F3E0C8)
}
Err(e) => {
println!("Error: {e}");
}
}
|
Beta Was this translation helpful? Give feedback.
Answered by
HalidOdat
Jul 28, 2023
Replies: 1 comment 3 replies
-
We can use the The 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().is_some());
}
Err(e) => {
println!("Error: {e}");
}
} |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
mlafeldt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We can use the
JsPromise::from_object()
static method for this after extracting the object from theJsValue
result, then use theJsPromise::state()
method to get the state of the promiseenum PromiseState
The
JsPromise::from_object()
doc, has a good example :)Adjusted to the given code, it would look something like this: