18c339a94Sopenharmony_ci# Asynchronous operations
28c339a94Sopenharmony_ci
38c339a94Sopenharmony_ciNode.js native add-ons often need to execute long running tasks and to avoid
48c339a94Sopenharmony_ciblocking the **event loop** they have to run them asynchronously from the
58c339a94Sopenharmony_ci**event loop**.
68c339a94Sopenharmony_ciIn the Node.js model of execution the event loop thread represents the thread
78c339a94Sopenharmony_ciwhere JavaScript code is executing. The Node.js guidance is to avoid blocking
88c339a94Sopenharmony_ciother work queued on the event loop thread. Therefore, we need to do this work on
98c339a94Sopenharmony_cianother thread.
108c339a94Sopenharmony_ci
118c339a94Sopenharmony_ciAll this means that native add-ons need to leverage async helpers from libuv as
128c339a94Sopenharmony_cipart of their implementation. This allows them to schedule work to be executed
138c339a94Sopenharmony_ciasynchronously so that their methods can return in advance of the work being
148c339a94Sopenharmony_cicompleted.
158c339a94Sopenharmony_ci
168c339a94Sopenharmony_ciNode Addon API provides an interface to support functions that cover
178c339a94Sopenharmony_cithe most common asynchronous use cases. There is an abstract classes to implement
188c339a94Sopenharmony_ciasynchronous operations:
198c339a94Sopenharmony_ci
208c339a94Sopenharmony_ci- **[`Napi::AsyncWorker`](async_worker.md)**
218c339a94Sopenharmony_ci
228c339a94Sopenharmony_ciThis class helps manage asynchronous operations through an abstraction
238c339a94Sopenharmony_ciof the concept of moving data between the **event loop** and **worker threads**.
248c339a94Sopenharmony_ci
258c339a94Sopenharmony_ciAlso, the above class may not be appropriate for every scenario. When using any
268c339a94Sopenharmony_ciother asynchronous mechanism, the following API is necessary to ensure an
278c339a94Sopenharmony_ciasynchronous operation is properly tracked by the runtime:
288c339a94Sopenharmony_ci
298c339a94Sopenharmony_ci- **[AsyncContext](async_context.md)**
308c339a94Sopenharmony_ci
318c339a94Sopenharmony_ci- **[CallbackScope](callback_scope.md)**
32