Differential D18758 Diff 58398 kdevplatform/3rdparty/qtpromise/qtpromise-0.5.0/docs/qtpromise/helpers/attempt.md
Changeset View
Changeset View
Standalone View
Standalone View
kdevplatform/3rdparty/qtpromise/qtpromise-0.5.0/docs/qtpromise/helpers/attempt.md
- This file was added.
1 | --- | ||||
---|---|---|---|---|---|
2 | title: attempt | ||||
3 | --- | ||||
4 | | ||||
5 | # QtPromise::attempt | ||||
6 | | ||||
7 | *Since: 0.4.0* | ||||
8 | | ||||
9 | ```cpp | ||||
10 | QtPromise::attempt(Functor functor, Args...) -> QPromise<R> | ||||
11 | | ||||
12 | // With: | ||||
13 | // - Functor: Function(Args...) -> R | QPromise<R> | ||||
14 | ``` | ||||
15 | | ||||
16 | Calls `functor` immediately and returns a promise fulfilled with the value returned by `functor`. Any synchronous exceptions will be turned into rejections on the returned promise. This is a convenient method that can be used instead of handling both synchronous and asynchronous exception flows. | ||||
17 | | ||||
18 | The type `R` of the `output` promise depends on the type returned by the `functor` function. If `functor` returns a promise (or `QFuture`), the `output` promise is delayed and will be resolved by the returned promise. | ||||
19 | | ||||
20 | ```cpp | ||||
21 | QPromise<QByteArray> download(const QUrl& url); | ||||
22 | | ||||
23 | QPromise<QByteArray> process(const QUrl& url) | ||||
24 | { | ||||
25 | return QtPromise::attempt([&]() { | ||||
26 | if (!url.isValid()) { | ||||
27 | throw InvalidUrlException(); | ||||
28 | } | ||||
29 | | ||||
30 | return download(url); | ||||
31 | } | ||||
32 | } | ||||
33 | | ||||
34 | auto output = process(url); | ||||
35 | | ||||
36 | // 'output' type: QPromise<QByteArray> | ||||
37 | output.then([](const QByteArray& res) { | ||||
38 | // {...} | ||||
39 | }).fail([](const InvalidUrlException& err) { | ||||
40 | // {...} | ||||
41 | }); | ||||
42 | ``` |