mirror of
https://github.com/simonbrunel/qtpromise.git
synced 2025-07-03 15:55:25 +08:00
Rename qPromise() helper to QtPromise::resolve()
For consistency with other helpers, deprecate `qPromise()` in favor of `QtPromise::resolve()` but also add support for calling this helper with lvalue. Add extra unit tests to make sure that rvalue is not copied.
This commit is contained in:
@ -51,7 +51,7 @@ module.exports = {
|
||||
'qtpromise/helpers/each',
|
||||
'qtpromise/helpers/filter',
|
||||
'qtpromise/helpers/map',
|
||||
'qtpromise/helpers/qpromise',
|
||||
'qtpromise/helpers/resolve',
|
||||
'qtpromise/helpers/qpromiseall'
|
||||
]
|
||||
},
|
||||
|
@ -31,7 +31,7 @@
|
||||
* [`QtPromise::each`](helpers/each.md)
|
||||
* [`QtPromise::filter`](helpers/filter.md)
|
||||
* [`QtPromise::map`](helpers/map.md)
|
||||
* [`qPromise`](helpers/qpromise.md)
|
||||
* [`QtPromise::resolve`](helpers/resolve.md)
|
||||
* [`qPromiseAll`](helpers/qpromiseall.md)
|
||||
|
||||
## Exceptions
|
||||
@ -39,3 +39,7 @@
|
||||
* [`QPromiseCanceledException`](exceptions/canceled.md)
|
||||
* [`QPromiseTimeoutException`](exceptions/timeout.md)
|
||||
* [`QPromiseUndefinedException`](exceptions/undefined.md)
|
||||
|
||||
## Deprecations
|
||||
|
||||
* `QtPromise::qPromise`: use [`QtPromise::resolve`](helpers/resolve.md) instead (since 0.5.0)
|
||||
|
@ -9,7 +9,7 @@ title: QPromiseCanceledException
|
||||
This exception is thrown for promise created from a [`QFuture`](../qtconcurrent.md) which has been canceled (e.g. using [`QFuture::cancel()`](http://doc.qt.io/qt-5/qfuture.html#cancel)), for example:
|
||||
|
||||
```cpp
|
||||
auto output = qPromise(future)
|
||||
auto output = QtPromise::resolve(future)
|
||||
.fail([](const QPromiseCanceledException&) {
|
||||
// `future` has been canceled!
|
||||
});
|
||||
|
@ -56,7 +56,7 @@ The following method `uncompress` data in a separate thread and returns a [promi
|
||||
```cpp
|
||||
QPromise<Entries> uncompress(const QByteArray& data)
|
||||
{
|
||||
return qPromise(QtConcurrent::run([](const QByteArray& data) {
|
||||
return QtPromise::resolve(QtConcurrent::run([](const QByteArray& data) {
|
||||
Entries entries;
|
||||
|
||||
// {...} uncompress data and parse content.
|
||||
|
@ -1,21 +0,0 @@
|
||||
---
|
||||
title: qPromise
|
||||
---
|
||||
|
||||
# qPromise
|
||||
|
||||
*Since: 0.1.0*
|
||||
|
||||
```
|
||||
qPromise(T value) -> QPromise<R>
|
||||
```
|
||||
|
||||
Similar to the [`QPromise<T>::resolve`](../qpromise/resolve.md) static method, creates a promise resolved from a given `value` without the extra typing:
|
||||
|
||||
```cpp
|
||||
auto promise = qPromise(); // QPromise<void>
|
||||
auto promise = qPromise(42); // QPromise<int>
|
||||
auto promise = qPromise(QString("foo")); // QPromise<QString>
|
||||
```
|
||||
|
||||
This method also allows to convert `QFuture<T>` to `QPromise<T>` delayed until the `QFuture` is finished ([read more](../qtconcurrent.md#convert)).
|
21
docs/qtpromise/helpers/resolve.md
Normal file
21
docs/qtpromise/helpers/resolve.md
Normal file
@ -0,0 +1,21 @@
|
||||
---
|
||||
title: resolve
|
||||
---
|
||||
|
||||
# QtPromise::resolve
|
||||
|
||||
*Since: 0.5.0*
|
||||
|
||||
```
|
||||
QtPromise::resolve(T value) -> QPromise<R>
|
||||
```
|
||||
|
||||
Similar to the [`QPromise<T>::resolve`](../qpromise/resolve.md) static method, creates a promise resolved from a given `value` but without the extra typing:
|
||||
|
||||
```cpp
|
||||
auto promise = QtPromise::resolve(); // QPromise<void>
|
||||
auto promise = QtPromise::resolve(42); // QPromise<int>
|
||||
auto promise = QtPromise::resolve(QString("foo")); // QPromise<QString>
|
||||
```
|
||||
|
||||
This method also allows to convert `QFuture<T>` to `QPromise<T>`, delayed until the `QFuture` is finished ([read more](../qtconcurrent.md#convert)).
|
@ -25,4 +25,4 @@ QPromise<int> compute(const QString& type)
|
||||
}
|
||||
```
|
||||
|
||||
See also: [`qPromise`](../helpers/qpromise.md)
|
||||
See also: [`QtPromise::resolve`](../helpers/resolve.md)
|
||||
|
@ -15,7 +15,7 @@ This method holds the execution of the remaining code until the `input` promise
|
||||
```cpp
|
||||
int result = -1;
|
||||
|
||||
QPromise<int> input = qPromise(QtConcurrent::run([]() {
|
||||
QPromise<int> input = QtPromise::resolve(QtConcurrent::run([]() {
|
||||
return 42;
|
||||
})).tap([&](int res) {
|
||||
result = res;
|
||||
|
@ -4,7 +4,7 @@ QtPromise integrates with [QtConcurrent](https://doc.qt.io/qt-5/qtconcurrent-ind
|
||||
|
||||
## <a name="qtconcurrent-convert"></a> Convert
|
||||
|
||||
Converting `QFuture<T>` to `QPromise<T>` is done using the [`qPromise`](helpers/qpromise.md) helper:
|
||||
Converting `QFuture<T>` to `QPromise<T>` is done using the [`QtPromise::resolve`](helpers/resolve.md) helper:
|
||||
|
||||
```cpp
|
||||
QFuture<int> future = QtConcurrent::run([]() {
|
||||
@ -12,13 +12,13 @@ QFuture<int> future = QtConcurrent::run([]() {
|
||||
return 42;
|
||||
});
|
||||
|
||||
QPromise<int> promise = qPromise(future);
|
||||
QPromise<int> promise = QtPromise::resolve(future);
|
||||
```
|
||||
|
||||
or simply:
|
||||
|
||||
```cpp
|
||||
auto promise = qPromise(QtConcurrent::run([]() {
|
||||
auto promise = QtPromise::resolve(QtConcurrent::run([]() {
|
||||
// {...}
|
||||
}));
|
||||
```
|
||||
|
Reference in New Issue
Block a user