You can attach your own .then and .catch callbacks if the script's
return value is a promise (which is what the return value of an async
function really is):
if (result->IsPromise()) {
Local<Promise> promise = result.As<Promise>();
if (promise->State() == Promise::kPending) {
Local<Function> resolve = Function::New(context,
Resolve).ToLocalChecked();
Local<Function> reject = Function::New(context, Reject).ToLocalChecked();
promise = promise.Then(resolve, reject).ToLocalChecked();
} else {
result = promise->Result(); // note: distinguish between
kFulfilled and kRejected
// ...
}
You'll need to pump the event loop and/or the microtask queue in order
for the promise to resolve.
Apropos the await keyword, that isn't accepted at the top-level scope
(outside an async function) unless you're executing the script as an
ES module and the --harmony_top_level_await flag is set.