Implementing Asynchronous Operations in OpenHarmony NAPI with Callbacks and Promises
This article builds upon the foundation of porting third-party libraries to OpenHarmony NAPI. We'll modify the hellonapi.cpp and index.ets files to explore asynchronous operations using Promises and Callbacks within the NAPI framework. This guide includes three examples: a Callback-based asynchronous interface, a Promise-based asynchronous inte ...
Posted on Sun, 20 Sep 2026 16:19:52 +0000 by scliburn
Data Type Conversion Between C/C++ and JavaScript in NAPI Framework
NAPI Data Type Convresion Overview
OpenHarmony NAPI encapsulates ECMAScript standard data types including Boolean, Null, Undefined, Number, BigInt, String, Symbol, Object, and Function into the unified napi_value type. This type handles data exchange between ArkUI applications and native code.
This implementation demonstrates a synchronous Add( ...
Posted on Mon, 14 Sep 2026 16:52:30 +0000 by wiggly81
Calling ArkTS Functions from Non-ArkTS Threads in HarmonyOS
Module Registration
First, we need to register the native module. This is done in the queue_work.cpp file:
// queue_work.cpp
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor desc[] = {
{ "queueWork", nullptr, QueueWork, nullptr, nullptr, nullptr, napi_default, nullptr }
...
Posted on Fri, 21 Aug 2026 16:48:40 +0000 by Tonsil
HarmonyOS N-API Module Loading in Main Thread
Module Loading Overview
The Node-API napi_load_module intreface enables module loading in the main thread. After loading, developers can retrieve exported properties using napi_get_property or access functions via napi_get_named_property. Supported scenarios include:
Loading system modules (e.g., @ohos.hilog)
Loading modules from ArkTS files
...
Posted on Fri, 31 Jul 2026 16:08:29 +0000 by himnbandit
Exporting C++ Class Objects via Node-API and Managing Their Lifecycle
Sample Project Source Code Analysis
The sample project uses a Native C++ template with the Stage model. This analysis focuses on key implementation files for class object export and lifecycle management.
Class Definition and Method Implementation
NapiTest.h Header File
#ifndef __NAPI_TEST_H__
#define __NAPI_TEST_H__
#include "napi/native_ ...
Posted on Thu, 11 Jun 2026 18:28:49 +0000 by itworks
Exporting NAPI Class Objects and Their Lifecycle Management (Part 1)
1. Exporting NAPI Class Objects
OpenHarmony NAPI provides a way to "wrap" C++ classes and instances sothat JS applications can call constructors, methods, and access properties of those classes.
Node.js Node-API also supports exporting class objects.
1.1. Flow of Exporting Class Objects via NAPI
Define a JS class using napi_define_cl ...
Posted on Thu, 07 May 2026 14:48:49 +0000 by bubblocity