I'm trying to call a nodejs async function from c++ that returns a promise that will be fufilled later using napi
Napi::Value napiStatus = this->nodejsFunction.Call(someArgs)
I want to wait until the promise is finished and get napiStatus to be filled out with the resolved value rather than the handle to the promise. Is there any way to do this? I've gotten this to work when the function is not async and simply returns a value but my current requirements won't let me do that.
Here's an example function in JS that I want to call
function timedTest() {
let timerExpired = new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve(1);
},3000)
})
return timerExpired;
}
I want napiStatus to be the resolved value of 1 (after napi conversions).
You have to do the same as if you were doing it from JavaScript: call the
.then()method of the Promise and register a callback.Here is the complete example:
The
Callbackfunction will receive the data from the resolved Promise: