Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    Announcements
    4
    8
    6213
    Loading More Posts
    • 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.
    • simbru
      simbru last edited by 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
      });
      
      kshegunov 1 Reply Last reply Reply Quote 3
      • kshegunov
        kshegunov Moderators @simbru last edited by

        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 Reply Quote 3
        • D
          danianlevan last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 1
          • simbru
            simbru last edited by simbru

            QtPromise 0.3.0 is now available!

            Release Notes
            Documentation
            Download

            1 Reply Last reply Reply Quote 1
            • simbru
              simbru last edited by

              QtPromise 0.4.0 is now available!

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

              Release Notes
              Documentation
              Download

              1 Reply Last reply Reply Quote 3
              • H
                h00bs last edited by h00bs

                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 Reply Quote 0
                • simbru
                  simbru last edited by

                  QtPromise 0.5.0 is now available!

                  New features: Qt signals support, reduce()

                  Release Notes
                  Documentation
                  Download

                  1 Reply Last reply Reply Quote 1
                  • simbru
                    simbru last edited by

                    QtPromise 0.6.0 is now available!

                    New feature: CMake support

                    Release Notes
                    Documentation
                    Download

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post