simonbrunel/qtpromise

No .fail without .then?

pwuertz opened this issue · 3 comments

In contrast to the QPromise::reject promise that is being tested in tst_fail, regular non-void promises fail to compile .fail methods being attached directly to p:

auto p = QPromise<int>([&](
    const QPromiseResolve<int>& resolve,
    const QPromiseReject<int>& reject)
{
    resolve(42);
    reject(43);
});
p.fail([](int x) {
    Q_UNUSED(x);
});
...qtpromise/src/qtpromise/qpromise_p.h:604: error: no matching function for call to ‘QtPromisePrivate::PromiseData<int>::resolve()’
             promise->m_d->resolve();
             ^~~~~~~

If .fail is preceded by a dummy .then method, the fail works:

p.then([](){}).fail([](int x) {
    Q_UNUSED(x);
});

That's weird, it should work, maybe broken by fa987a5 or 7b0cba5. Does it work with 0.3?

Actually, fail() is supposed to recover an error and thus must return the "replacement" value required to continue the promise chain:

auto p = QPromise<int>([&](auto resolve, auto reject) {
    reject(43);
});

p.fail([](int x) {
    Q_UNUSED(x);
    return -1;
});

Oh, that makes sense. So just a misunderstanding on my part, thanks for clearing that up.

If I happen to find a good spot for a static_assert with a fitting message for such a mistake I'll let you know.