add finally method

https://github.com/BlackMATov/promise.hpp/issues/33
This commit is contained in:
BlackMATov
2020-01-01 03:26:41 +07:00
parent 00b102f82c
commit 661b30b395
2 changed files with 155 additions and 0 deletions

View File

@@ -416,6 +416,21 @@ namespace promise_hpp
[](auto&& v) { return std::forward<decltype(v)>(v); },
std::forward<RejectF>(on_reject));
}
//
// finally
//
template < typename FinallyF >
promise<T> finally(FinallyF&& on_finally) {
return then([f = on_finally](auto&& v) {
std::invoke(std::move(f));
return std::forward<decltype(v)>(v);
}, [f = on_finally](std::exception_ptr e) -> T {
std::invoke(std::move(f));
std::rethrow_exception(e);
});
}
private:
class state;
std::shared_ptr<state> state_;
@@ -837,6 +852,20 @@ namespace promise_hpp
[](){},
std::forward<RejectF>(on_reject));
}
//
// finally
//
template < typename FinallyF >
promise<void> finally(FinallyF&& on_finally) {
return then([f = on_finally]() {
std::invoke(std::move(f));
}, [f = on_finally](std::exception_ptr e) {
std::invoke(std::move(f));
std::rethrow_exception(e);
});
}
private:
class state;
std::shared_ptr<state> state_;