Commit 8c88885a authored by Kenton Varda's avatar Kenton Varda

Add Promise<T>::ignoreResult() helper for ignoring return value.

This is largely motivated by the fact that Clang currently miscompiles `.then([](auto){})`:

https://llvm.org/bugs/show_bug.cgi?id=24989
parent 907d36fb
......@@ -285,6 +285,24 @@ TEST(Async, DeepChain4) {
promise.wait(waitScope);
}
TEST(Async, IgnoreResult) {
EventLoop loop;
WaitScope waitScope(loop);
bool done = false;
Promise<void> promise = Promise<int>(123).then([&](int i) {
done = true;
return i + 321;
}).ignoreResult();
EXPECT_FALSE(done);
promise.wait(waitScope);
EXPECT_TRUE(done);
}
TEST(Async, SeparateFulfiller) {
EventLoop loop;
WaitScope waitScope(loop);
......
......@@ -194,6 +194,11 @@ public:
// actual I/O. To solve this, use `kj::evalLater()` to yield control; this way, all other events
// in the queue will get a chance to run before your callback is executed.
Promise<void> ignoreResult() KJ_WARN_UNUSED_RESULT { return then([](T&&) {}); }
// Convenience method to convert the promise to a void promise by ignoring the return value.
//
// You must still wait on the returned promise if you want the task to execute.
template <typename ErrorFunc>
Promise<T> catch_(ErrorFunc&& errorHandler) KJ_WARN_UNUSED_RESULT;
// Equivalent to `.then(identityFunc, errorHandler)`, where `identifyFunc` is a function that
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment