Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Announcements
  4. QtPromise 0.6.0 - Promises/A+ for Qt/C++
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Announcements
8 Posts 4 Posters 7.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • simbruS Offline
    simbruS Offline
    simbru
    wrote on last edited by simbru
    #1

    Latest release: 0.6.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 :)

    Example

    #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) {
                    resolve(reply->readAll());
                } else {
                    reject(reply->error());
                }
                reply->deleteLater();
            });
        });
    }
    

    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
    });
    
    kshegunovK 1 Reply Last reply
    3
    • simbruS simbru

      Latest release: 0.6.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 :)

      Example

      #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) {
                      resolve(reply->readAll());
                  } else {
                      reject(reply->error());
                  }
                  reply->deleteLater();
              });
          });
      }
      

      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
      });
      
      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

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

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      3
      • D Offline
        D Offline
        danianlevan
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        1
        • simbruS Offline
          simbruS Offline
          simbru
          wrote on last edited by simbru
          #4

          QtPromise 0.3.0 is now available!

          Release Notes
          Documentation
          Download

          1 Reply Last reply
          1
          • simbruS Offline
            simbruS Offline
            simbru
            wrote on last edited by
            #5

            QtPromise 0.4.0 is now available!

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

            Release Notes
            Documentation
            Download

            1 Reply Last reply
            3
            • H Offline
              H Offline
              h00bs
              wrote on last edited by h00bs
              #6

              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
                  .get("http://httpbin.org/get")
                  .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
                  });
                }
              }
              

              Developer at Felgo - https://felgo.com/qt

              Develop mobile Apps for iOS & Android with Qt
              Develop Games with Qt

              Felgo is an official Qt Technology Partner

              1 Reply Last reply
              0
              • simbruS Offline
                simbruS Offline
                simbru
                wrote on last edited by
                #7

                QtPromise 0.5.0 is now available!

                New features: Qt signals support, reduce()

                Release Notes
                Documentation
                Download

                1 Reply Last reply
                1
                • simbruS Offline
                  simbruS Offline
                  simbru
                  wrote on last edited by
                  #8

                  QtPromise 0.6.0 is now available!

                  New feature: CMake support

                  Release Notes
                  Documentation
                  Download

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved