mirror of
https://github.com/simonbrunel/qtpromise.git
synced 2025-07-05 16:55:23 +08:00
Implement QPromise::tap(handler)
This `handler` allows to observe the value of the `input` promise, without changing the propagated value. The `output` promise will be resolved with the same value as the `input` promise (the `handler` returned value will be ignored). However, if `handler` throws, `output` is rejected with the new exception. Unlike `finally`, this handler is not called for rejections.
This commit is contained in:
@ -43,6 +43,9 @@ public:
|
||||
template <typename THandler>
|
||||
inline QPromise<T> finally(THandler handler) const;
|
||||
|
||||
template <typename THandler>
|
||||
inline QPromise<T> tap(THandler handler) const;
|
||||
|
||||
inline QPromise<T> wait() const;
|
||||
|
||||
void swap(QPromiseBase<T>& other) { qSwap(m_d, other.m_d); }
|
||||
|
@ -139,6 +139,16 @@ inline QPromise<T> QPromiseBase<T>::finally(THandler handler) const
|
||||
});
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename THandler>
|
||||
inline QPromise<T> QPromiseBase<T>::tap(THandler handler) const
|
||||
{
|
||||
QPromise<T> p = *this;
|
||||
return p.then(handler).then([=]() {
|
||||
return p;
|
||||
});
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline QPromise<T> QPromiseBase<T>::wait() const
|
||||
{
|
||||
|
Reference in New Issue
Block a user