QtPromise 0.6.0 - Promises/A+ for Qt/C++
-
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 asthen()
,fail()
andfinally()
(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 });
-
This post is deleted!
-
QtPromise 0.3.0 is now available!
-
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 }); } }
-
QtPromise 0.5.0 is now available!
New features: Qt signals support, reduce()
-