Browse Source

Add a JsPromise::from_result for convenience (#4039)

pull/4041/head
Hans Larsen 1 month ago committed by GitHub
parent
commit
e892d94f8d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 41
      core/engine/src/object/builtins/jspromise.rs

41
core/engine/src/object/builtins/jspromise.rs

@ -311,6 +311,47 @@ impl JsPromise {
promise
}
/// Creates a new `JsPromise` from a `Result<T, JsError>`, where `T` is the fulfilled value of
/// the promise, and `JsError` is the rejection reason. This is a simpler way to create a
/// promise that is either fulfilled or rejected based on the result of a computation.
///
/// # Examples
///
/// ```
/// # use std::error::Error;
/// # use boa_engine::{
/// # object::builtins::JsPromise,
/// # builtins::promise::PromiseState,
/// # Context, JsResult, JsString, js_string, js_error
/// # };
/// let context = &mut Context::default();
///
/// fn do_thing(success: bool) -> JsResult<JsString> {
/// success.then(|| js_string!("resolved!")).ok_or(js_error!("rejected!"))
/// }
///
/// let promise = JsPromise::from_result(do_thing(true), context);
/// assert_eq!(
/// promise.state(),
/// PromiseState::Fulfilled(js_string!("resolved!").into())
/// );
///
/// let promise = JsPromise::from_result(do_thing(false), context);
/// assert_eq!(
/// promise.state(),
/// PromiseState::Rejected(js_string!("rejected!").into())
/// );
/// ```
pub fn from_result<V: Into<JsValue>, E: Into<JsError>>(
value: Result<V, E>,
context: &mut Context,
) -> Self {
match value {
Ok(v) => Self::resolve(v, context),
Err(e) => Self::reject(e, context),
}
}
/// Resolves a `JsValue` into a `JsPromise`.
///
/// Equivalent to the [`Promise.resolve()`] static method.

Loading…
Cancel
Save