Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait for a Promise to resolve in c++ #4329

Closed
funkyp1ckle opened this issue Jan 16, 2024 · 1 comment
Closed

Wait for a Promise to resolve in c++ #4329

funkyp1ckle opened this issue Jan 16, 2024 · 1 comment
Labels
needs more info issues that need more info from the author stalled issues that did not receive response for a month

Comments

@funkyp1ckle
Copy link

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

@RedYetiDev
Copy link
Member

Hi! Please reformat your question to make use of markdown features, like code blocks, to make it more readable. Thank you!

@RedYetiDev RedYetiDev added the needs more info issues that need more info from the author label Jun 10, 2024
@RedYetiDev RedYetiDev closed this as not planned Won't fix, can't repro, duplicate, stale Jul 20, 2024
@RedYetiDev RedYetiDev added the stalled issues that did not receive response for a month label Jul 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs more info issues that need more info from the author stalled issues that did not receive response for a month
Projects
None yet
Development

No branches or pull requests

2 participants