// C++线程函数void ProcessFunction(napi_env env, napi_ref callbackRef, void* data) { // 进行耗时操作,获取处理结果 int result = 42;
// 创建异步任务上下文 napi_handle_scope handleScope; napi_open_handle_scope(env, &handleScope);
napi_async_context asyncContext; napi_create_async_context(env, nullptr, nullptr, &asyncContext); napi_open_async_scope(env, asyncContext, &asyncScope);
// 获取回调函数 napi_value callback; napi_get_reference_value(env, callbackRef, &callback); // 创建返回结果 napi_value resultValue; napi_create_int32(env, result, &resultValue);
// 调用回调函数,并传递结果 napi_value arguments[] = { resultValue }; napi_call_function(env, asyncContext, callback, 1, arguments, nullptr);
// 释放资源 napi_close_async_scope(env, asyncScope); napi_close_handle_scope(env, handleScope);}
评论