From 3ac928b90e7dd3f11f92490edac283c6e2bc6fda Mon Sep 17 00:00:00 2001 From: Determinant Date: Sun, 11 Feb 2018 19:28:09 -0500 Subject: add rejection example --- README.rst | 8 ++++---- promise.hpp | 4 ++-- test.cpp | 28 ++++++++++++++++++++++------ test_ref.txt | 5 ++++- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/README.rst b/README.rst index 76591e6..c1d9088 100644 --- a/README.rst +++ b/README.rst @@ -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 promise_t promise::all(PList promise_list); + template 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 promise_t promise::race(PList promise_list); + template 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 { public: friend Promise; - template friend promise_t all(PList promise_list); - template friend promise_t race(PList promise_list); + template friend promise_t all(const PList &promise_list); + template friend promise_t race(const PList &promise_list); promise_t() = delete; template diff --git a/test.cpp b/test.cpp index 737512e..3c2bfc9 100644 --- a/test.cpp +++ b/test.cpp @@ -97,10 +97,6 @@ int main() { }); auto pm7 = promise::all(std::vector{pm1, pm6}) - .fail([](int reason) { - printf("reason: %d\n", reason); - return reason; - }) .then([](const promise::values_t values) { int x = any_cast(values[1]); printf("promise 1, 6 resolved %d\n", x); @@ -113,10 +109,30 @@ int main() { auto pm9 = promise::race(std::vector{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(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 -- cgit v1.2.3