nodex_api/value/
promise.rs

1use crate::{api, prelude::*};
2use std::{marker::PhantomData, mem::MaybeUninit, os::raw::c_char};
3
4#[derive(Debug, Copy, Clone)]
5pub struct JsPromise<L: NapiValueT, R: NapiValueT>(
6    pub(crate) JsValue,
7    pub(crate) napi_deferred,
8    PhantomData<L>,
9    PhantomData<R>,
10);
11
12impl<L: NapiValueT + Copy, R: NapiValueT + Copy> JsPromise<L, R> {
13    pub(crate) fn from_raw(value: JsValue, deferred: napi_deferred) -> JsPromise<L, R> {
14        JsPromise(value, deferred, PhantomData, PhantomData)
15    }
16
17    pub fn env(&self) -> NapiEnv {
18        self.0.env()
19    }
20
21    pub fn raw(&self) -> napi_value {
22        self.0.raw()
23    }
24
25    pub fn value(&self) -> JsValue {
26        self.0
27    }
28
29    /// This API creates a deferred object and a JavaScript promise.
30    pub fn new(env: NapiEnv) -> NapiResult<JsPromise<L, R>> {
31        let mut deferred = MaybeUninit::uninit();
32
33        let promise = napi_call!(
34            =napi_create_promise,
35            env,
36            deferred.as_mut_ptr(),
37        );
38
39        let deferred = unsafe { deferred.assume_init() };
40
41        Ok(Self::from_raw(JsValue(env, promise), deferred))
42    }
43
44    /// This API resolves a JavaScript promise by way of the deferred object with which it is
45    /// associated. Thus, it can only be used to resolve JavaScript promises for which the
46    /// corresponding deferred object is available. This effectively means that the promise must
47    /// have been created using napi_create_promise() and the deferred object returned from that
48    /// call must have been retained in order to be passed to this API.
49    ///
50    /// The deferred object is freed upon successful completion.
51    pub fn resolve(&self, resolution: L) -> NapiResult<()> {
52        napi_call!(napi_resolve_deferred, self.env(), self.1, resolution.raw())
53    }
54
55    /// This API rejects a JavaScript promise by way of the deferred object with which it is
56    /// associated. Thus, it can only be used to reject JavaScript promises for which the
57    /// corresponding deferred object is available. This effectively means that the promise
58    /// must have been created using napi_create_promise() and the deferred object returned
59    /// from that call must have been retained in order to be passed to this API.
60    ///
61    /// The deferred object is freed upon successful completion.
62    pub fn reject(&self, rejection: R) -> NapiResult<()> {
63        napi_call!(napi_reject_deferred, self.env(), self.1, rejection.raw())
64    }
65}
66
67impl<L: NapiValueT + Copy + 'static, R: NapiValueT + Copy + 'static> JsPromise<L, R> {
68    /// Spawn a busy task in the libuv pool.
69    pub fn spawn<T>(
70        env: NapiEnv,
71        mut work: impl FnMut(&mut T) + Send + 'static,
72        mut complete: impl FnMut(Self, NapiStatus, T) -> NapiResult<()> + 'static,
73    ) -> NapiResult<JsPromise<L, R>>
74    where
75        T: Default,
76    {
77        let promise: JsPromise<L, R> = JsPromise::new(env)?;
78        env.async_work(
79            "napi-promise-task",
80            T::default(),
81            move |state| work(state),
82            // NB: execute in the main js thread.
83            move |_, status, state| complete(promise, status, state),
84        )?
85        .queue()?;
86
87        Ok(promise)
88    }
89}
90
91impl<L: NapiValueT + Copy, R: NapiValueT + Copy> NapiValueCheck for JsPromise<L, R> {
92    fn check(&self) -> NapiResult<bool> {
93        Ok(napi_call!(=napi_is_promise, self.env(), self.raw()))
94    }
95}