diff options
author | Determinant <[email protected]> | 2018-02-11 19:28:09 -0500 |
---|---|---|
committer | Determinant <[email protected]> | 2018-02-11 19:28:09 -0500 |
commit | 3ac928b90e7dd3f11f92490edac283c6e2bc6fda (patch) | |
tree | 93a6753afa56774c9b8ecac2cfaabd7979b99bdd | |
parent | 9da19de63af8876e8cfaed4abe5a9a6f957986a7 (diff) |
add rejection example
-rw-r--r-- | README.rst | 8 | ||||
-rw-r--r-- | promise.hpp | 4 | ||||
-rw-r--r-- | test.cpp | 28 | ||||
-rw-r--r-- | test_ref.txt | 5 |
4 files changed, 32 insertions, 13 deletions
@@ -48,7 +48,7 @@ argument. promise_t::resolve() const; -Resolve the promise without empty result. This may trigger the other promises +Resolve the promise with empty result. This may trigger the other promises waiting for the current promise recursively. When a promise is triggered, the registered ``on_fulfilled()`` function should be expecting no argument, otherwise a type mismatch is thrown. @@ -57,7 +57,7 @@ otherwise a type mismatch is thrown. promise_t::reject() const; -Reject the promise without empty reason. This may reject the other promises +Reject the promise with empty reason. This may reject the other promises waiting for the current promise recursively. When a promise is rejected, the registered ``on_rejected()`` function should be expecting on argument, otherwise a type mismatch is thrown. @@ -93,7 +93,7 @@ the current promise. .. code-block:: cpp - template<typename PList> promise_t promise::all(PList promise_list); + template<typename PList> promise_t promise::all(const PList &promise_list); Create a promise waiting for the asynchronous resolution of all promises in ``promise_list``. The result for the created promise will be typed @@ -104,7 +104,7 @@ promises. .. code-block:: cpp - template<typename PList> promise_t promise::race(PList promise_list); + template<typename PList> promise_t promise::race(const PList &promise_list); Create a promise waiting for the asynchronous resolution of any promises in ``promise_list``. The result for the created promise will be the result from diff --git a/promise.hpp b/promise.hpp index 7831a1c..9fb6170 100644 --- a/promise.hpp +++ b/promise.hpp @@ -124,8 +124,8 @@ namespace promise { class promise_t: public std::shared_ptr<Promise> { public: friend Promise; - template<typename PList> friend promise_t all(PList promise_list); - template<typename PList> friend promise_t race(PList promise_list); + template<typename PList> friend promise_t all(const PList &promise_list); + template<typename PList> friend promise_t race(const PList &promise_list); promise_t() = delete; template<typename Func> @@ -97,10 +97,6 @@ int main() { }); auto pm7 = promise::all(std::vector<promise_t>{pm1, pm6}) - .fail([](int reason) { - printf("reason: %d\n", reason); - return reason; - }) .then([](const promise::values_t values) { int x = any_cast<int>(values[1]); printf("promise 1, 6 resolved %d\n", x); @@ -113,10 +109,30 @@ int main() { auto pm9 = promise::race(std::vector<promise_t>{pm7, pm8}) .then([](promise::pm_any_t value) { - printf("finally, promise 9 resolved with %d\n", + printf("promise 9 resolved with %d\n", any_cast<int>(value)); + }) + .then([]() { + puts("rejecting with value -1"); + return promise_t([](promise_t pm) { + pm.reject(-1); + }); + }) + .then([]() { + puts("this line should not appear in the output"); + }) + .then([](int) { + puts("this line should not appear in the outputs"); + }) + .fail([](int reason) { + printf("reason: %d\n", reason); + return reason + 1; + }).then([](){ + puts("this line should not appear in the outputs"); + }, + [](int reason) { + printf("reason: %d\n", reason); }); - puts("calling t4: resolve promise 3"); t4(); puts("calling t5: resolve promise 4"); diff --git a/test_ref.txt b/test_ref.txt index 142ba93..574a8b3 100644 --- a/test_ref.txt +++ b/test_ref.txt @@ -23,4 +23,7 @@ void return is ok void parameter is ok void parameter will ignore the returned value promise 1, 6 resolved 100 -finally, promise 9 resolved with 101 +promise 9 resolved with 101 +rejecting with value -1 +reason: -1 +reason: 0 |