I have a synchronous C++ Node.js addon function that does an heavy operation:
Napi::Object SyncHeavyFunction(const Napi::CallbackInfo& info) {
std::this_thread::sleep_for(std::chrono::seconds(50));
...
return env.Null();
}
I'm trying to run it asynchronously wrapping it into a javascript Promise:
console.log("before creating the Promise");
let p = new Promise((resolve) => {
const out = SyncHeavyFunction();
reolve(out)
});
console.log("after creating the Promise");
However it seems that the creation of the Promise blocks until the underlying native function terminates. I'm wondering if this behavior is expected and which is the best way to achieve the asynchronous call of a synchronous native function only using javascript code.