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:
Simon Brunel
2017-08-23 18:12:57 +02:00
parent 25d2bad54f
commit c55fa03e7b
5 changed files with 183 additions and 0 deletions

View File

@ -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); }

View File

@ -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
{