Simon Brunel eebcb4f364 Migrate documentation to VuePress
GitBook development seems a bit stuck right now so let's switch to VuePress which, IMO, is more user friendly. Update the documentation to include the version number in which features were added and use custom container to display notes and warnings.
2018-09-09 10:55:07 +02:00

1.5 KiB

title
.each

QPromise::each

Since: 0.4.0

QPromise<Sequence<T>>::each(Functor functor) -> QPromise<Sequence<T>>

// With:
// - Sequence: STL compatible container
// - Functor: Function(T value, int index) -> any

::: warning IMPORTANT This method only applies to promise with sequence value. :::

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.

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.

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