Implement QPromise<Sequence<T>>::each(functor)

Call the given `functor` on each element in the promise value (i.e. `Sequence<T>`), then resolve to the original sequence unmodified. Also provide a static helper to directly filter values (`QtPromise::each(values, functor)`).
This commit is contained in:
Simon Brunel
2018-05-25 08:55:20 +02:00
parent f610826ef0
commit 051fed5fbc
13 changed files with 457 additions and 0 deletions

View File

@ -5,6 +5,7 @@
* [API Reference](qtpromise/api-reference.md)
* [QPromise](qtpromise/qpromise/constructor.md)
* [.delay](qtpromise/qpromise/delay.md)
* [.each](qtpromise/qpromise/each.md)
* [.fail](qtpromise/qpromise/fail.md)
* [.filter](qtpromise/qpromise/filter.md)
* [.finally](qtpromise/qpromise/finally.md)
@ -23,5 +24,6 @@
* [qPromise](qtpromise/helpers/qpromise.md)
* [qPromiseAll](qtpromise/helpers/qpromiseall.md)
* [QtPromise::attempt](qtpromise/helpers/attempt.md)
* [QtPromise::each](qtpromise/helpers/each.md)
* [QtPromise::filter](qtpromise/helpers/filter.md)
* [QtPromise::map](qtpromise/helpers/map.md)

View File

@ -4,6 +4,7 @@
* [`QPromise<T>::QPromise`](qpromise/constructor.md)
* [`QPromise<T>::delay`](qpromise/delay.md)
* [`QPromise<T>::each`](qpromise/each.md)
* [`QPromise<T>::fail`](qpromise/fail.md)
* [`QPromise<T>::filter`](qpromise/filter.md)
* [`QPromise<T>::finally`](qpromise/finally.md)
@ -28,5 +29,6 @@
* [`qPromise`](helpers/qpromise.md)
* [`qPromiseAll`](helpers/qpromiseall.md)
* [`QtPromise::attempt`](helpers/attempt.md)
* [`QtPromise::each`](helpers/each.md)
* [`QtPromise::filter`](helpers/filter.md)
* [`QtPromise::map`](helpers/map.md)

View File

@ -0,0 +1,39 @@
## `QtPromise::each`
```cpp
QtPromise::each(Sequence<T> values, Functor functor) -> QPromise<Sequence<T>>
// With:
// - Sequence: STL compatible container (e.g. QVector, etc.)
// - Functor: Function(T value, int index) -> void | QPromise<void>
```
Calls the given `functor` on each element in `values` then resolves to the original sequence
unmodified. If `functor` throws, `output` is rejected with the new exception.
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.
```cpp
auto output = QtPromise::each(QVector<QUrl>{
QUrl("http://a..."),
QUrl("http://b..."),
QUrl("http://c...")
}, [](const QUrl& url, ...) {
return QPromise<void>([&](auto resolve, auto reject) {
// process url asynchronously ...
})
});
// `output` resolves as soon as all promises returned by
// `functor` are fulfilled or at least one is rejected.
// output type: QPromise<QVector<QUrl>>
output.then([](const QVector<QUrl>& res) {
// 'res' contains the original values
});
```
See also: [`QPromise<T>::each`](../qpromise/each.md)

View File

@ -0,0 +1,48 @@
## `QPromise<Sequence<T>>::each`
> **Important:** applies only to promise with sequence value.
```cpp
QPromise<Sequence<T>>::each(Functor functor) -> QPromise<Sequence<T>>
// With:
// - Sequence: STL compatible container
// - Functor: Function(T value, int index) -> any
```
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.
```cpp
QPromise<QList<QByteArray>> input = {...}
auto output = input.each([](const QByteArray& value, int index) {
// process value ...
});
// output type: QPromise<QList<QByteArray>>
output.then([](const QList<QByteArray>& res) {
// 'res' contains the original values
});
```
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.
```cpp
QPromise<QList<QUrl>> input = {...}
auto output = input.each([](const QUrl& url, ...) {
return QPromise<void>([&](auto resolve, auto reject) {
// process url asynchronously ...
})
});
// `output` resolves as soon as all promises returned by
// `functor` are fulfilled or at least one is rejected.
// output type: QPromise<QList<QUrl>>
output.then([](const QList<QUrl>& res) {
// 'res' contains the original values
});
```
See also: [`QtPromise::each`](../helpers/each.md)