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::Local promise = result.As();
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::Local();
}
return escapeHandleScope.Escape(result);
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::Local promise = result.As();
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::Local();
}
return escapeHandleScope.Escape(result);
Operating system
Windows
Scope
c++ embedding promise
Module and version
libnode