首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在N-Api插件C中解析Node.js Promise

如何在N-Api插件C中解析Node.js Promise
EN

Stack Overflow用户
提问于 2020-08-06 18:16:04
回答 2查看 354关注 0票数 3

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

index.js

代码语言:javascript
复制
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

代码语言:javascript
复制
#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)

在addon.cpp中,我想调用异步JS函数并获取返回值

我使用此文档作为示例https://nodejs.org/api/n-api.html#n_api_promises

EN

回答 2

Stack Overflow用户

发布于 2020-08-07 13:55:24

下面的堆栈溢出帖子的答案也解释了这种情况。在答案中,延迟承诺的resolve/reject是在CompleteMyPromise1()函数中完成的。

How to create async function using NAPI that return Promises

票数 1
EN

Stack Overflow用户

发布于 2022-01-21 17:42:32

您必须执行与在JavaScript中相同的操作:调用Promise的.then()方法并注册一个回调。

下面是完整的示例:

代码语言:javascript
复制
Napi::Value ret = asyncFunction.Call({});
if (!ret.IsPromise())
  throw Napi::Error::New(env, "your function did not return a Promise");
Napi::Promise promise = ret.As<Napi::Promise>();

Napi::Value thenValue = promise.Get("then");
if (!thenValue.IsFunction())
  throw Napi::Error::New(env, "Promise is not thenable");
Napi::Function then = thenValue.As<Napi::Function>();

Napi::Function callback = Napi::Function::New(env, Callback, "cpp_callback");
then.Call(promise, {callback});

Callback函数将从解析的Promise接收数据:

代码语言:javascript
复制
Napi::Value Callback(const Napi::CallbackInfo &info) { 
  printf("Callback called\n");
  printf("Data: %s\n", info[0].ToString().Utf8Value().c_str());
  return info.Env().Null();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63281395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档