Struct neon::types::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(TaskContext<'_>) -> 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(TaskContext<'_>) -> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.