Implement QPromise::delay(msec)

This method returns a promise that will be fulfilled with the same value as the `input` promise and after at least `msec` milliseconds. If the `input` promise is rejected, the `output` promise is immediately rejected with the same reason.
This commit is contained in:
Simon Brunel
2017-08-24 18:28:44 +02:00
parent c55fa03e7b
commit b47ca0569e
4 changed files with 60 additions and 0 deletions

View File

@ -46,6 +46,7 @@ public:
template <typename THandler>
inline QPromise<T> tap(THandler handler) const;
inline QPromise<T> delay(int msec) const;
inline QPromise<T> wait() const;
void swap(QPromiseBase<T>& other) { qSwap(m_d, other.m_d); }

View File

@ -1,6 +1,7 @@
// Qt
#include <QCoreApplication>
#include <QSharedPointer>
#include <QTimer>
namespace QtPromise {
@ -149,6 +150,16 @@ inline QPromise<T> QPromiseBase<T>::tap(THandler handler) const
});
}
template <typename T>
inline QPromise<T> QPromiseBase<T>::delay(int msec) const
{
return tap([=]() {
return QPromise<void>([&](const QPromiseResolve<void>& resolve) {
QTimer::singleShot(msec, resolve);
});
});
}
template <typename T>
inline QPromise<T> QPromiseBase<T>::wait() const
{