A handle that settles a JavaScript Promise.
A Resolver lets native code settle a JavaScript Promise from any thread. Taking a Resolver as the trailing parameter of a bound function turns it into an asynchronous call that returns a Promise to the page immediately.
Pass the resolver to a background thread and settle it when the work finishes:
RunOnSaveThread([path, done = std::move(done)] {
if (WriteSave(path))
done.Resolve(true);
else
});
};
static Error Make(ErrorType type, std::string message)
Create a native error of any type.
Definition Error.h:168
A handle that settles a JavaScript Promise.
Definition Resolver.h:97
@ Error
A plain Error, or an error type with no dedicated constant.
Definition Error.h:55
Page script awaits the returned Promise normally:
const saved = await app.save("slot1.dat");
Settling the Promise
Resolvers follow these rules:
Sharing a Resolver
Resolver is move-only and can't be captured directly by copyable types such as std::function. Wrap the resolver in std::shared_ptr to pass it into a copyable callback or job queue:
auto shared = std::make_shared<js::Resolver>(std::move(done));
jobs.push_back([shared] { shared->Resolve(42); });
Promises Outside Bound Functions
Call js::Context::MakePromise() on the page's context to create a pending Promise directly without a bound function:
auto [promise, resolver] = ctx.MakePromise();
ctx["ready"] = promise;
resolver.Resolve(std::string("ok"));
- Note
- If the page navigates away or closes before the Promise settles, calling Resolve() or Reject() is safe and the library discards the result.
- Note
- Use a Resolver when an outside system or background thread signals completion of an operation. For sequential multi-step async operations, use js::Task instead.
- See also
- js::Task, js::Context::MakePromise(), js::Error