QtPromise 0.5.0 - Promises/A+ for Qt/C++

  • Latest release: 0.5.0 - documentation

    I recently published a C++ implementation of promise that leverages Qt classes. It follows as far as possible the Promises/A+ specifications with an API mostly inspired by Bluebird and Q. QtPromise is a header-only library that only depends on QtCore (5.4+) with C++11 support enabled (read more).

    QtPromise consists of a main template class, QPromise<T> (T being the promise value type), providing the usual continuation methods such as then(), fail() and finally() (all of them being processed asynchronously). Errors are handled as exceptions and thus can be of any type. QtPromise also integrates with QFuture and is thread-safe. You can learn more about this project on GitHub (MIT licence). Feedback and contributions are welcome :)


    #include <QtPromise>
    using namespace QtPromise;

    This download function creates a promise from callbacks which will be resolved when the network request is finished:

    QPromise<QByteArray> download(const QUrl& url)
        return QPromise<QByteArray>([&](
            const QPromiseResolve<QByteArray>& resolve,
            const QPromiseReject<QByteArray>& reject) {
            QNetworkReply* reply = manager->get(QNetworkRequest(url));
            QObject::connect(reply, &QNetworkReply::finished, [=]() {
                if (reply->error() == QNetworkReply::NoError) {
                } else {

    The following method uncompress data in a separate thread and returns a promise from QFuture:

    QPromise<Entries> uncompress(const QByteArray& data)
        return qPromise(QtConcurrent::run([](const QByteArray& data) {
            Entries entries;
            // {...} uncompress data and parse content.
            if (error) {
                throw MalformedException();
            return entries;
        }, data));

    It's then easy to chain the whole asynchronous process using promises:

    • initiate the promise chain by downloading a specific URL,
    • then and only if download succeeded, uncompress received data,
    • then validate and process the uncompressed entries,
    • finally perform operations whatever the process succeeded or failed,
    • and handle specific errors using fail.
    download(url).then(&uncompress).then([](const Entries& entries) {
        if (entries.isEmpty()) {
            throw UpdateException("No entries");
        // {...} process entries
    }).finally([]() {
        // {...} cleanup
    }).fail([](QNetworkReply::NetworkError err) {
        // {...} handle network error
    }).fail([](const UpdateException& err) {
        // {...} handle update error
    }).fail([]() {
        // {...} catch all

  • Qt Champions 2017

    Thanks for that!
    Have also a look at @benlau's work here. Perhaps you could even exchange experience/ideas.

  • This post is deleted!

  • QtPromise 0.3.0 is now available!

    Release Notes

  • QtPromise 0.4.0 is now available!

    New features: map(), filter(), each(), tapFail(), attempt()

    Release Notes

  • Well done! This looks very "promising", hah, you got the joke!

    If anyone is also looking to use promises with QML, here is an QML implementation: https://v-play.net/updates/release-2-18-1-javascript-promises-for-rest-services-tinder-swipe-material-cards-qml-qsortfilterproxymodel-qml-youtube-player

    import VPlayApps 1.0
    import QtQuick 2.0
    App {
      Component.onCompleted: {
        var p1 = Promise.resolve(3);
        var p2 = 1337;
        var p3 = HttpRequest
        .then(function(resp) {
          return resp.body;
        var p4 = Promise.all([p1, p2, p3]);
        p4.then(function(values) {
          console.log(values[0]); // 3
          console.log(values[1]); // 1337
          console.log(values[2]); // resp.body

  • QtPromise 0.5.0 is now available!

    New features: Qt signals support, reduce()

    Release Notes

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.