Wait for a Promise to resolve in c++ #4329
Labels
needs more info
issues that need more info from the author
stalled
issues that did not receive response for a month
Details
I am embedding nodejs into a c++ application. I need the result of a asynchronous nodejs function call. I am returned a promise from nodejs. Is there a way to wait for the promise to resolve before continuing the program.
Node.js version
Not applicable.
Example code
v8::EscapableHandleScope escapeHandleScope(isolate);
auto checkedCode = v8::String::NewFromUtf8(isolate, source.c_str()).ToLocalChecked();
auto script = v8::Script::Compile(localContext, checkedCode).ToLocalChecked();
v8::TryCatch tryCatch(isolate);
auto result = script->Run(localContext).ToLocalChecked();
if (result.IsEmpty()) {
std::cerr << "Running Script has unhandled exception" << std::endl;
std::cerr << *v8::String::Utf8Value(isolate, tryCatch.Exception()) << std::endl;
exit(ErrorCode::PROCESSING_ERROR);
}
if (result->IsPromise()) {
v8::Localv8::Promise promise = result.Asv8::Promise();
while (promise->State() == v8::Promise::kPending) {
//isolate->PerformMicrotaskCheckpoint();
uv_run(node::GetCurrentEventLoop(isolate), UV_RUN_ONCE);
}
result = promise->State() == v8::Promise::kFulfilled ? promise->Result() : v8::Localv8::Value();
}
return escapeHandleScope.Escape(result);
Operating system
Windows
Scope
c++ embedding promise
Module and version
libnode
The text was updated successfully, but these errors were encountered: