neon::types

Struct Deferred

Source
pub struct Deferred { /* private fields */ }
Expand description

A controller struct that can be used to resolve or reject a JsPromise.

It is recommended to settle a Deferred with Deferred::settle_with to ensure exceptions are caught.

On Node-API versions less than 6, dropping a Deferred without settling will cause a panic. On Node-API 6+, the associated JsPromise will be automatically rejected.

§Examples

See JsPromise, JsFuture.

Implementations§

Source§

impl Deferred

Source

pub fn resolve<'a, V, C>(self, cx: &mut C, value: Handle<'_, V>)
where V: Value, C: Context<'a>,

Resolve a JsPromise with a JavaScript value

Source

pub fn reject<'a, V, C>(self, cx: &mut C, value: Handle<'_, V>)
where V: Value, C: Context<'a>,

Reject a JsPromise with a JavaScript value

Source

pub fn try_settle_with<V, F>( self, channel: &Channel, complete: F, ) -> Result<JoinHandle<()>, SendError>
where V: Value, F: FnOnce(Cx<'_>) -> JsResult<'_, V> + Send + 'static,

Available on crate feature napi-4 only.

Settle the JsPromise by sending a closure across a Channel to be executed on the main JavaScript thread.

Usage is identical to Deferred::settle_with.

Returns a SendError if sending the closure to the main JavaScript thread fails. See Channel::try_send for more details.

Source

pub fn settle_with<V, F>(self, channel: &Channel, complete: F) -> JoinHandle<()>
where V: Value, F: FnOnce(Cx<'_>) -> JsResult<'_, V> + Send + 'static,

Available on crate feature napi-4 only.

Settle the JsPromise by sending a closure across a Channel to be executed on the main JavaScript thread.

Panics if there is a libuv error.

let channel = cx.channel();
let (deferred, promise) = cx.promise();

deferred.settle_with(&channel, move |mut cx| Ok(cx.number(42)));

Trait Implementations§

Source§

impl Drop for Deferred

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.