Commit 648fd368 authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook Github Bot

Task::start with a callback

Summary: This is useful to avoid extra allocations when integrating with code that doesn't use folly::futures.

Reviewed By: yfeldblum

Differential Revision: D13812866

fbshipit-source-id: aa76b8cda43d1a781e058f6edbe2ecb8100268de
parent 504981eb
...@@ -204,18 +204,26 @@ class FOLLY_NODISCARD TaskWithExecutor { ...@@ -204,18 +204,26 @@ class FOLLY_NODISCARD TaskWithExecutor {
Promise<lift_unit_t<T>> p; Promise<lift_unit_t<T>> p;
auto sf = p.getSemiFuture(); auto sf = p.getSemiFuture();
[](Promise<lift_unit_t<T>> p, std::move(*this).start([promise = std::move(p)](Try<T>&& result) mutable {
TaskWithExecutor task) -> detail::InlineTaskDetached { promise.setTry(std::move(result));
});
return sf;
}
// Start execution of this task eagerly and call the callback when complete.
template <typename F>
void start(F&& tryCallback) && {
[](TaskWithExecutor task,
std::decay_t<F> cb) -> detail::InlineTaskDetached {
try { try {
p.setTry(co_await std::move(task).co_awaitTry()); cb(co_await std::move(task).co_awaitTry());
} catch (const std::exception& e) { } catch (const std::exception& e) {
p.setException(exception_wrapper(std::current_exception(), e)); cb(Try<T>(exception_wrapper(std::current_exception(), e)));
} catch (...) { } catch (...) {
p.setException(exception_wrapper(std::current_exception())); cb(Try<T>(exception_wrapper(std::current_exception())));
} }
}(std::move(p), std::move(*this)); }(std::move(*this), std::forward<F>(tryCallback));
return sf;
} }
template <typename ResultCreator> template <typename ResultCreator>
......
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