Differential D18758 Diff 58398 kdevplatform/3rdparty/qtpromise/qtpromise-0.5.0/docs/qtpromise/qpromise/each.md
Changeset View
Changeset View
Standalone View
Standalone View
kdevplatform/3rdparty/qtpromise/qtpromise-0.5.0/docs/qtpromise/qpromise/each.md
- This file was added.
1 | --- | ||||
---|---|---|---|---|---|
2 | title: .each | ||||
3 | --- | ||||
4 | | ||||
5 | # QPromise::each | ||||
6 | | ||||
7 | *Since: 0.4.0* | ||||
8 | | ||||
9 | ```cpp | ||||
10 | QPromise<Sequence<T>>::each(Functor functor) -> QPromise<Sequence<T>> | ||||
11 | | ||||
12 | // With: | ||||
13 | // - Sequence: STL compatible container | ||||
14 | // - Functor: Function(T value, int index) -> any | ||||
15 | ``` | ||||
16 | | ||||
17 | ::: warning IMPORTANT | ||||
18 | This method only applies to promise with sequence value. | ||||
19 | ::: | ||||
20 | | ||||
21 | Calls the given `functor` on each element in the promise value (i.e. `Sequence<T>`), then resolves to the original sequence unmodified. If `functor` throws, `output` is rejected with the new exception. | ||||
22 | | ||||
23 | ```cpp | ||||
24 | QPromise<QList<QByteArray>> input = {...} | ||||
25 | | ||||
26 | auto output = input.each([](const QByteArray& value, int index) { | ||||
27 | // process value ... | ||||
28 | }); | ||||
29 | | ||||
30 | // output type: QPromise<QList<QByteArray>> | ||||
31 | output.then([](const QList<QByteArray>& res) { | ||||
32 | // 'res' contains the original values | ||||
33 | }); | ||||
34 | ``` | ||||
35 | | ||||
36 | If `functor` returns a promise (or `QFuture`), the `output` promise is delayed until all the promises are resolved. If any of the promises fail, `output` immediately rejects with the error of the promise that rejected, whether or not the other promises are resolved. | ||||
37 | | ||||
38 | ```cpp | ||||
39 | QPromise<QList<QUrl>> input = {...} | ||||
40 | | ||||
41 | auto output = input.each([](const QUrl& url, ...) { | ||||
42 | return QPromise<void>([&](auto resolve, auto reject) { | ||||
43 | // process url asynchronously ... | ||||
44 | }) | ||||
45 | }); | ||||
46 | | ||||
47 | // `output` resolves as soon as all promises returned by | ||||
48 | // `functor` are fulfilled or at least one is rejected. | ||||
49 | | ||||
50 | // output type: QPromise<QList<QUrl>> | ||||
51 | output.then([](const QList<QUrl>& res) { | ||||
52 | // 'res' contains the original values | ||||
53 | }); | ||||
54 | ``` | ||||
55 | | ||||
56 | See also: [`QtPromise::each`](../helpers/each.md) |