18c339a94Sopenharmony_ci# AsyncContext
28c339a94Sopenharmony_ci
38c339a94Sopenharmony_ciThe [Napi::AsyncWorker](async_worker.md) class may not be appropriate for every
48c339a94Sopenharmony_ciscenario. When using any other async mechanism, introducing a new class
58c339a94Sopenharmony_ci`Napi::AsyncContext` is necessary to ensure an async operation is properly
68c339a94Sopenharmony_citracked by the runtime. The `Napi::AsyncContext` class can be passed to
78c339a94Sopenharmony_ci[Napi::Function::MakeCallback()](function.md) method to properly restore the
88c339a94Sopenharmony_cicorrect async execution context.
98c339a94Sopenharmony_ci
108c339a94Sopenharmony_ci## Methods
118c339a94Sopenharmony_ci
128c339a94Sopenharmony_ci### Constructor
138c339a94Sopenharmony_ci
148c339a94Sopenharmony_ciCreates a new `Napi::AsyncContext`.
158c339a94Sopenharmony_ci
168c339a94Sopenharmony_ci```cpp
178c339a94Sopenharmony_ciexplicit Napi::AsyncContext::AsyncContext(napi_env env, const char* resource_name);
188c339a94Sopenharmony_ci```
198c339a94Sopenharmony_ci
208c339a94Sopenharmony_ci- `[in] env`: The environment in which to create the `Napi::AsyncContext`.
218c339a94Sopenharmony_ci- `[in] resource_name`: Null-terminated strings that represents the
228c339a94Sopenharmony_ciidentifier for the kind of resource that is being provided for diagnostic
238c339a94Sopenharmony_ciinformation exposed by the `async_hooks` API.
248c339a94Sopenharmony_ci
258c339a94Sopenharmony_ci### Constructor
268c339a94Sopenharmony_ci
278c339a94Sopenharmony_ciCreates a new `Napi::AsyncContext`.
288c339a94Sopenharmony_ci
298c339a94Sopenharmony_ci```cpp
308c339a94Sopenharmony_ciexplicit Napi::AsyncContext::AsyncContext(napi_env env, const char* resource_name, const Napi::Object& resource);
318c339a94Sopenharmony_ci```
328c339a94Sopenharmony_ci
338c339a94Sopenharmony_ci- `[in] env`: The environment in which to create the `Napi::AsyncContext`.
348c339a94Sopenharmony_ci- `[in] resource_name`: Null-terminated strings that represents the
358c339a94Sopenharmony_ciidentifier for the kind of resource that is being provided for diagnostic
368c339a94Sopenharmony_ciinformation exposed by the `async_hooks` API.
378c339a94Sopenharmony_ci- `[in] resource`: Object associated with the asynchronous operation that
388c339a94Sopenharmony_ciwill be passed to possible `async_hooks`.
398c339a94Sopenharmony_ci
408c339a94Sopenharmony_ci### Destructor
418c339a94Sopenharmony_ci
428c339a94Sopenharmony_ciThe `Napi::AsyncContext` to be destroyed.
438c339a94Sopenharmony_ci
448c339a94Sopenharmony_ci```cpp
458c339a94Sopenharmony_civirtual Napi::AsyncContext::~AsyncContext();
468c339a94Sopenharmony_ci```
478c339a94Sopenharmony_ci
488c339a94Sopenharmony_ci### Env
498c339a94Sopenharmony_ci
508c339a94Sopenharmony_ciRequests the environment in which the async context has been initially created.
518c339a94Sopenharmony_ci
528c339a94Sopenharmony_ci```cpp
538c339a94Sopenharmony_ciNapi::Env Env() const;
548c339a94Sopenharmony_ci```
558c339a94Sopenharmony_ci
568c339a94Sopenharmony_ciReturns the `Napi::Env` environment in which the async context has been created.
578c339a94Sopenharmony_ci
588c339a94Sopenharmony_ci## Operator
598c339a94Sopenharmony_ci
608c339a94Sopenharmony_ci```cpp
618c339a94Sopenharmony_ciNapi::AsyncContext::operator napi_async_context() const;
628c339a94Sopenharmony_ci```
638c339a94Sopenharmony_ci
648c339a94Sopenharmony_ciReturns the Node-API `napi_async_context` wrapped by the `Napi::AsyncContext`
658c339a94Sopenharmony_ciobject. This can be used to mix usage of the C Node-API and node-addon-api.
668c339a94Sopenharmony_ci
678c339a94Sopenharmony_ci## Example
688c339a94Sopenharmony_ci
698c339a94Sopenharmony_ci```cpp
708c339a94Sopenharmony_ci#include "napi.h"
718c339a94Sopenharmony_ci
728c339a94Sopenharmony_civoid MakeCallbackWithAsyncContext(const Napi::CallbackInfo& info) {
738c339a94Sopenharmony_ci  Napi::Function callback = info[0].As<Napi::Function>();
748c339a94Sopenharmony_ci  Napi::Object resource = info[1].As<Napi::Object>();
758c339a94Sopenharmony_ci
768c339a94Sopenharmony_ci  // Create a new async context instance.
778c339a94Sopenharmony_ci  Napi::AsyncContext context(info.Env(), "async_context_test", resource);
788c339a94Sopenharmony_ci
798c339a94Sopenharmony_ci  // Invoke the callback with the async context instance.
808c339a94Sopenharmony_ci  callback.MakeCallback(Napi::Object::New(info.Env()),
818c339a94Sopenharmony_ci      std::initializer_list<napi_value>{}, context);
828c339a94Sopenharmony_ci
838c339a94Sopenharmony_ci  // The async context instance is automatically destroyed here because it's
848c339a94Sopenharmony_ci  // block-scope like `Napi::HandleScope`.
858c339a94Sopenharmony_ci}
868c339a94Sopenharmony_ci```
87