C++ promise/A+ library in Javascript style.
散了吧,cpp20 coroutine了,这么代码已经没有意义 -- 作者
Promise-cpp is library that implements promise/A+ standard, which can be the base component in event-looped asynchronized programming. It is NOT std::promise.
Similar to Javascript Promise API.
Type safety: the resolved/rejected arguments can be captured by the "then" function with same arguments type.
Exceptions supports: cpp exception will be received by the "on_rejected" function.
Optional header-only configuration enabled with the PROMISE_HEADONLY macro
Easy to use, just #include "promise-cpp/promise.hpp" is enough, code based on standard c++11 syntax, no external dependencies required.
Easy to integrate with other libararies (see examples of asio, qt and mfc).
Useful extended functions on promise object: doWhile, raceAndResolve, raceAndReject
example/test0.cpp: a simple test code for promise resolve/reject operations. (no dependencies)
example/simple_timer.cpp: simple promisified timer. (no dependencies)
example/simple_benchmark_test.cpp: benchmark test for simple promisified asynchronized tasks. (no dependencies)
example/asio_timer.cpp: promisified timer based on asio callback timer. (boost::asio required)
example/asio_benchmark_test.cpp: benchmark test for promisified asynchronized tasks in asio. (boost::asio required)
example/asio_http_client.cpp: promisified flow for asynchronized http client. (boost::asio, boost::beast required)
example/asio_http_server.cpp: promisified flow for asynchronized http server. (boost::asio, boost::beast required)
example/qt_timer: promisified timer in QT gui thread. (QT required)
example/mfc_timer: promisified timer in windows MFC gui thread.
Please use cmake to build from CMakeLists.txt.
The library has passed test on these compilers --
gcc 5
Visual studio 2015 sp3
clang 3.4.2
To use as header only library, just define macro PROMISE_HEADONLY when compiling.
cmake /path/to/promise_sourcecmake -DPROMISE_BUILD_SHARED=ON /path/to/promise_sourceSome of the examples use boost::asio as io service, and use boost::beast as http service. You need to boost_1_66 or higher to build these examples.
For examples, you can build with boost library --
> cmake -DBOOST_ROOT=/path/to/boost_source /path/to/promise_sourceExample of asio http client. (full code here)
int main(int argc, char** argv) {
// The io_context is required for all I/O
asio::io_context ioc;
// Launch the asynchronous operation
download(ioc, "http://www.163.com/")
.then([&]() {
return download(ioc, "http://baidu.com/");
}).then([&]() {
return download(ioc, "http://qq.com");
}).then([&]() {
return download(ioc, "http://github.com/xhawk18");
});
// Run the I/O service. The call will return when
// the get operation is complete.
ioc.run();
return 0;
}This sample code shows converting a timer callback to promise object.
…Creates a new promise object with a user-defined function. The user-defined functions, used as parameters by newPromise, must have a parameter Defer d. for example --
return newPromise([](Defer d){
})Returns a promise that is resolved with the given value. for example --
return resolve(3, '2');Returns a promise that is rejected with the given arguments. for example --
return reject("some_error");Wait until all promise objects in "promise_list" are resolved or one of which is rejected. The "promise_list" can be any container that has promise object as element type.
for (Promise &promise : promise_list) { ... }
for example --
Promise d0 = newPromise([](Defer d){ /* ... */ });
Promise d1 = newPromise([](Defer d){ /* ... */ });
std::vector promise_list = { d0, d1 };
all(promise_list).then([](){
/* code here for all promise objects are resolved */
}).fail([](){
/* code here for one of the promise objects is rejected */
});Returns a promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects, with the value or reason from that promise. The "promise_list" can be any container that has promise object as element type.
for (Promise &promise : promise_list) { ... }
for example --
Promise d0 = newPromise([](Defer d){ /* ... */ });
Promise d1 = newPromise([](Defer d){ /* ... */ });
std::vector promise_list = { d0, d1 };
race(promise_list).then([](){
/* code here for one of the promise objects is resolved */
}).fail([](){
/* code here for one of the promise objects is rejected */
});Same as function race(), and reject all depending promises object in the list.
Same as function race(), and resove all depending promises object in the list.
"While loop" for promisied task. A promise object will passed as parameter when call func, which can be resolved to continue with the "while loop", or be rejected to break from the "while loop".
for example --
doWhile([](DeferLoop d){
// Add code here for your task in "while loop"
// Call "d.doContinue()" to continue with the "while loop",
// or call "d.doBreak()" to break from the "while loop", in this case,
// the returned promise object will be in resolved status.
});
Return the chaining promise object, where on_resolved is the function to be called when previous promise object was resolved, on_rejected is the function to be called when previous promise object was rejected. for example --
return newPromise([](Defer d){
d.resolve(9567, 'A');
}).then(
/* function on_resolved */ [](int n, char ch){
printf("%d %c\n", n, ch); //will print 9567 here
},
/* function on_rejected */ [](){
printf("promise rejected\n"); //will not run to here in this code
}
);Return the chaining promise object, where on_resolved is the function to be called when previous promise object was resolved. for example --
return newPromise([](Defer d){
d.resolve(9567);
}).then([](int n){
printf("%d\n", n); b //will print 9567 here
});Return the chaining promise object, where d is the callback function be called when previous promise object was resolved or rejected.
Return the chaining promise object, where d is the callback function be called when previous promise object was resolved or rejected.
Return the chaining promise object, where "promise" is the promise object be called when previous promise object was resolved or rejected.
Return the chaining promise object, where on_rejected is the function to be called when previous promise object was rejected.
This function is usually named "catch" in most implements of Promise library. https://www.promisejs.org/api/
In promise_cpp, function name "fail" is used instead of "catch", since "catch" is a keyword of c++.
for example --
return newPromise([](Defer d){
d.reject(-1, std::string("oh, no!"));
}).fail([](int err, string &str){
printf("%d, %s\n", err, str.c_str()); //will print "-1, oh, no!" here
});No open issues yet, or sync has not completed.