n-api

How to create async function using NAPI that return Promises

孤人 提交于 2020-12-25 19:02:40
问题 i am trying to create node module using NAPI .I have to create async function that returns promises. I don't want that testasynfunction will block NodeJS event loop. do_something_asynchronous is a synchronous function. napi_deferred do_something_synchronous(napi_env env,napi_deferred deferred){ printf("\n3) Function called"); //napi_deferred deferred; napi_value undefined; napi_status status; // Create a value with which to conclude the deferred. status = napi_get_undefined(env, &undefined);

How can you call an emitter callback from separate c++ thread in an addon?

不羁岁月 提交于 2020-01-04 05:08:09
问题 For context, I started with this question. I need to call the callback for the emitter in another thread. I made a minimal example but it segfaults on emit.Call({cb, result}); My first instinct is that I have a problem with the lifetimes of env or the emit function. addon.cpp #include <napi.h> #include <iostream> #include <thread> #include <memory> #include <functional> #include <chrono> std::shared_ptr<std::thread> thread; bool running = true; void generate(Napi::Env& env, Napi::Function&

Streaming data into a Node.js C++ Addon with N-API

老子叫甜甜 提交于 2019-12-12 11:22:03
问题 I am building a C++ addon for NodeJS and I want to stream data asynchronously from C++ to Node. I have found this article, https://nodeaddons.com/streaming-data-into-a-node-js-c-addon/, however; I want to use the N-API instead of NAN. I have been searching through the NodeJS docs and examples as well as looking for other resources and examples but have not come across a resource to show me how I can accomplish this. This is my first time writing a C++ addon for NodeJS. An example that would