2.0 KiB
Promise
The Napi::Promise
class, along with its
Napi::Promise::Deferred
class, implement the ability to
create, resolve, and reject Promise objects.
The basic approach is to create a
Napi::Promise::Deferred
object and return to your caller
the value returned by the Napi::Promise::Deferred::Promise
method. For example:
::Value YourFunction(const Napi::CallbackInfo& info) {
Napi// your code goes here...
::Promise::Deferred deferred = Napi::Promise::Deferred::New(info.Env());
Napi// deferred needs to survive this call...
return deferred.Promise();
}
Later, when the asynchronous process completes, call either the
Resolve
or Reject
method on the
Napi::Promise::Deferred
object created earlier:
.Resolve(String::New(info.Env(), "OK")); deferred
Promise::Deferred Methods
Factory Method
static Napi::Promise::Deferred Napi::Promise::Deferred::New(napi_env env);
[in] env
: Thenapi_env
environment in which to create theNapi::Promise::Deferred
object.
Constructor
::Promise::Deferred(napi_env env); Napi
[in] env
: Thenapi_env
environment in which to construct theNapi::Promise::Deferred
object.
Env
::Env Napi::Promise::Deferred::Env() const; Napi
Returns the Env environment this Napi::Promise::Deferred
object is associated with.
Promise
::Promise Napi::Promise::Deferred::Promise() const; Napi
Returns the Napi::Promise
object held by the
Napi::Promise::Deferred
object.
Resolve
void Napi::Promise::Deferred::Resolve(napi_value value) const;
Resolves the Napi::Promise
object held by the
Napi::Promise::Deferred
object.
[in] value
: The N-API primitive value with which to resolve theNapi::Promise
.
Reject
void Napi::Promise::Deferred::Reject(napi_value value) const;
Rejects the Promise object held by the
Napi::Promise::Deferred
object.
[in] value
: The N-API primitive value with which to reject theNapi::Promise
.