Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何解析N-Api插件C中的Node.js承诺_C++_Node.js_C_Node.js Addon_N Api - Fatal编程技术网

C++ 如何解析N-Api插件C中的Node.js承诺

C++ 如何解析N-Api插件C中的Node.js承诺,c++,node.js,c,node.js-addon,n-api,C++,Node.js,C,Node.js Addon,N Api,我的主要问题是从addon中的Node.js调用异步函数并获取返回值。 我正在尝试解析从被调用的JS函数返回的承诺 index.js const addon = require('./build/Debug/addon.node'); async function asunc_func() { return "Hello asunc_func"; } const result = addon.test_async(asunc_func); // addon ret

我的主要问题是从addon中的Node.js调用异步函数并获取返回值。 我正在尝试解析从被调用的JS函数返回的承诺

index.js

const addon = require('./build/Debug/addon.node');

async function asunc_func() {
    return "Hello asunc_func";
}

const result = addon.test_async(asunc_func); // addon returned a promise
result.then(res => {
    console.log(res);
})
addon.cpp

#include <napi.h>

using namespace Napi;

int do_something_asynchronous(napi_deferred deferred, Napi::Function func, Napi::Env env) {
    napi_value undefined;
    napi_status status;

    status = napi_get_undefined(env, &undefined);
    if (status != napi_ok) return 0;

    napi_value result = func.Call({});

    // I want to get the string "Hello asunc_func" here
    // from called JS function

    napi_valuetype * result_type;
    status = napi_typeof(env, result, result_type); // result_type: napi_object

    if (result) {
      status = napi_resolve_deferred(env, deferred, result);
    } else {
      status = napi_reject_deferred(env, deferred, undefined);
    }
    if (status != napi_ok) return 0;
    deferred = NULL;
}

napi_value test_async(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();

    Napi::Function func = info[0].As<Napi::Function>();

    napi_deferred deferred;
    napi_value promise;
    napi_status status;

    status = napi_create_promise(env, &deferred, &promise);
    if (status != napi_ok) return NULL;

    do_something_asynchronous(deferred, func, env);
    return promise;
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
  exports.Set(Napi::String::New(env, "test_async"), Napi::Function::New(env, test_async));
  return exports;
}

NODE_API_MODULE(addon, Init)
#包括
使用名称空间Napi;
int do_something_asynchronous(napi_deferred,napi::Function func,napi::Env Env){
napi_值未定义;
国家适应行动方案状况;
状态=napi\u get\u未定义(环境和未定义);
如果(状态!=napi_ok)返回0;
napi_值结果=函数调用({});
//我想在这里得到字符串“Hello asunc_func”
//从调用的JS函数
napi_值类型*结果_类型;
status=napi_-typeof(env,result,result_-type);//result_-type:napi_对象
如果(结果){
状态=napi_解决_延迟(环境、延迟、结果);
}否则{
状态=napi_拒绝_延迟(环境,延迟,未定义);
}
如果(状态!=napi_ok)返回0;
延迟=空;
}
napi_值测试_异步(const napi::CallbackInfo&info){
Napi::Env Env=info.Env();
Napi::函数func=info[0].As();
纳皮尤推迟;
napi_价值承诺;
国家适应行动方案状况;
状态=napi\u创建承诺(环境、延期和承诺);
如果(状态!=napi_ok)返回NULL;
做一些异步的事情(延迟、函数、环境);
回报承诺;
}
Napi::对象初始化(Napi::Env Env,Napi::对象导出){
exports.Set(Napi::String::New(env,“test_async”)、Napi::Function::New(env,test_async));
出口退税;
}
节点API模块(插件,初始化)
在addon.cpp中,我想调用async JS函数并获取返回值


我以这个文档为例

下面的堆栈溢出帖子也解释了这个场景。在该回答中,延迟承诺的解决/拒绝是在CompleteMyPromise1()函数中完成的


您调用
do\u something\u asynchronous()
的方式并不是真正的异步调用,它是一种阻塞调用,就像其他C函数调用一样。谢谢,有关事件循环阻塞的信息将对我有所帮助。但我需要一些不同的东西。在您的示例中,您将承诺从加载项返回到Node.js,Node.js应用程序将其解析。我需要相反的-Node.js将承诺传递给Addon并由Addon解决它。你知道怎么做吗?)