mirror of
https://github.com/simonbrunel/qtpromise.git
synced 2025-07-05 08:45:23 +08:00
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:
@ -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); }
|
||||
|
@ -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
|
||||
{
|
||||
|
Reference in New Issue
Block a user