mirror of
https://github.com/simonbrunel/qtpromise.git
synced 2024-11-21 18:24:29 +08:00
Fix docs C++ snippet for the .fail()
args
- Fix the wrong lambda format (missing `[]`). - Make consistent argument naming (`error`). - Use `const &` when appropriated.
This commit is contained in:
parent
3c1461b8d0
commit
cc29ef3512
@ -7,7 +7,7 @@ canceled (e.g. using [`QFuture::cancel()`](http://doc.qt.io/qt-5/qfuture.html#ca
|
|||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
auto output = QtPromise::resolve(future)
|
auto output = QtPromise::resolve(future)
|
||||||
.fail([](const QPromiseCanceledException&) {
|
.fail([](const QPromiseCanceledException& error) {
|
||||||
// `future` has been canceled!
|
// `future` has been canceled!
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
@ -8,7 +8,7 @@ This is the default exception thrown when reaching the time limit when using the
|
|||||||
```cpp
|
```cpp
|
||||||
QPromise<int> input = {...}
|
QPromise<int> input = {...}
|
||||||
auto output = input.timeout(2000)
|
auto output = input.timeout(2000)
|
||||||
.fail([](const QPromiseTimeoutException& e) {
|
.fail([](const QPromiseTimeoutException& error) {
|
||||||
// operation timed out after 2s!
|
// operation timed out after 2s!
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
@ -90,9 +90,9 @@ download(url).then(&uncompress).then([](const Entries& entries) {
|
|||||||
// {...} process entries
|
// {...} process entries
|
||||||
}).finally([]() {
|
}).finally([]() {
|
||||||
// {...} cleanup
|
// {...} cleanup
|
||||||
}).fail([](QNetworkReply::NetworkError err) {
|
}).fail([](QNetworkReply::NetworkError error) {
|
||||||
// {...} handle network error
|
// {...} handle network error
|
||||||
}).fail([](const UpdateException& err) {
|
}).fail([](const UpdateException& error) {
|
||||||
// {...} handle update error
|
// {...} handle update error
|
||||||
}).fail([]() {
|
}).fail([]() {
|
||||||
// {...} catch all
|
// {...} catch all
|
||||||
|
@ -41,7 +41,7 @@ auto output = process(url);
|
|||||||
// 'output' type: QPromise<QByteArray>
|
// 'output' type: QPromise<QByteArray>
|
||||||
output.then([](const QByteArray& res) {
|
output.then([](const QByteArray& res) {
|
||||||
// {...}
|
// {...}
|
||||||
}).fail([](const InvalidUrlException& err) {
|
}).fail([](const InvalidUrlException& error) {
|
||||||
// {...}
|
// {...}
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
@ -42,9 +42,9 @@ auto output = QtPromise::connect(sender, &Sender::finished, &Sender::error);
|
|||||||
// 'output' type: QPromise<QByteArray>
|
// 'output' type: QPromise<QByteArray>
|
||||||
output.then([](const QByteArray& res) {
|
output.then([](const QByteArray& res) {
|
||||||
// 'res' is the first argument of the 'finished' signal.
|
// 'res' is the first argument of the 'finished' signal.
|
||||||
}).fail([](ErrorCode err) {
|
}).fail([](ErrorCode error) {
|
||||||
// 'err' is the first argument of the 'error' signal.
|
// 'error' is the first argument of the 'error' signal.
|
||||||
}).fail([](const QPromiseContextException& err) {
|
}).fail([](const QPromiseContextException& error) {
|
||||||
// the 'sender' object has been destroyed before any of
|
// the 'sender' object has been destroyed before any of
|
||||||
// the 'finished' or 'error' signals have been emitted.
|
// the 'finished' or 'error' signals have been emitted.
|
||||||
});
|
});
|
||||||
|
@ -10,16 +10,19 @@ title: .fail
|
|||||||
QPromise<T>::fail(Function onRejected) -> QPromise<T>
|
QPromise<T>::fail(Function onRejected) -> QPromise<T>
|
||||||
```
|
```
|
||||||
|
|
||||||
Shorthand to `promise.then(nullptr, onRejected)`, similar to the [`catch` statement](http://en.cppreference.com/w/cpp/language/try_catch):
|
Shorthand to [`promise.then(nullptr, onRejected)`](then.md) for handling errors in promise chains,
|
||||||
|
similar to the native C++ [`catch` statement](http://en.cppreference.com/w/cpp/language/try_catch):
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
promise.fail([](const MyException&) {
|
promise.fail([](const MyException& error) {
|
||||||
// {...}
|
// {...}
|
||||||
}).fail(const QException&) {
|
}).fail([](const QException& error) {
|
||||||
// {...}
|
// {...}
|
||||||
}).fail(const std::exception&) {
|
}).fail([](const std::exception& error) {
|
||||||
// {...}
|
// {...}
|
||||||
}).fail() {
|
}).fail([]() {
|
||||||
// {...} catch-all
|
// {...} catch-all
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
See also: [`QPromise::then`](then.md)
|
||||||
|
@ -17,12 +17,12 @@ rejected with the new exception.
|
|||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
QPromise<int> input = {...}
|
QPromise<int> input = {...}
|
||||||
auto output = input.tapFail([](Error err) {
|
auto output = input.tapFail([](const Error& error) {
|
||||||
log(err);
|
log(error);
|
||||||
}).then([](int res) {
|
}).then([](int res) {
|
||||||
return process(res);
|
return process(res);
|
||||||
}).fail([](Error err) {
|
}).fail([](const Error& error) {
|
||||||
handle(err);
|
handle(error);
|
||||||
return -1;
|
return -1;
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
@ -21,7 +21,7 @@ auto output = input.timeout(2000)
|
|||||||
.then([](int res) {
|
.then([](int res) {
|
||||||
// operation succeeded within 2 seconds
|
// operation succeeded within 2 seconds
|
||||||
})
|
})
|
||||||
.fail([](const QPromiseTimeoutException& e) {
|
.fail([](const QPromiseTimeoutException& error) {
|
||||||
// operation timed out!
|
// operation timed out!
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
@ -67,7 +67,7 @@ promise.then([](int res) {
|
|||||||
|
|
||||||
return QString("42");
|
return QString("42");
|
||||||
});
|
});
|
||||||
}).fail(const CustomException& err) {
|
}).fail([](const CustomException& error) {
|
||||||
// {...}
|
// {...}
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
@ -53,7 +53,7 @@ auto output = QtPromise::connect(obj, &Object::finished, &Object::error);
|
|||||||
// output type: QPromise<QByteArray>
|
// output type: QPromise<QByteArray>
|
||||||
output.then([](const QByteArray& data) {
|
output.then([](const QByteArray& data) {
|
||||||
// {...}
|
// {...}
|
||||||
}).fail(const ObjectError& error) {
|
}).fail([](const ObjectError& error) {
|
||||||
// {...}
|
// {...}
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
@ -69,7 +69,7 @@ auto output = QtPromise::connect(obj, &Object::finished, &Object::error);
|
|||||||
// output type: QPromise<QByteArray>
|
// output type: QPromise<QByteArray>
|
||||||
output.then([]() {
|
output.then([]() {
|
||||||
// {...}
|
// {...}
|
||||||
}).fail(const QPromiseUndefinedException& error) {
|
}).fail([](const QPromiseUndefinedException& error) {
|
||||||
// {...}
|
// {...}
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
@ -84,7 +84,7 @@ auto output = QtPromise::connect(objA, &ObjectA::finished, objB, &ObjectB::error
|
|||||||
// output type: QPromise<QByteArray>
|
// output type: QPromise<QByteArray>
|
||||||
output.then([](const QByteArray& data) {
|
output.then([](const QByteArray& data) {
|
||||||
// {...}
|
// {...}
|
||||||
}).fail(const ObjectBError& error) {
|
}).fail([](const ObjectBError& error) {
|
||||||
// {...}
|
// {...}
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user