Public Types | |
typedef std::exception_ptr | FailureType |
typedef std::function< void()> | OnResolveHandler |
typedef std::function< void(FailureType const &)> | OnRejectHandler |
typedef std::function< void(OnResolveHandler const &)> | BasicInitializer |
typedef std::function< void(OnResolveHandler const &, OnRejectHandler const &)> | ComplexInitializer |
Public Member Functions | |
Promise (BasicInitializer const &initializer) noexcept | |
Promise (ComplexInitializer const &initializer) noexcept | |
Promise () noexcept | |
Promise (FailureType const &rejected) noexcept | |
void | resolve () |
void | reject (FailureType const &rejected) |
template<typename OnSuccess , typename PromiseType = typename std::result_of<OnSuccess()>::type> | |
detail::GetPromiseType< PromiseType >::type | Then (OnSuccess const &onSuccess) noexcept |
template<typename OnSuccess , typename OnFailure , typename PromiseType1 = typename std::result_of<OnSuccess()>::type, typename PromiseType2 = typename std::result_of<OnFailure(FailureType const&)>::type> | |
detail::GetPromiseType< PromiseType1 >::type | Then (OnSuccess const &onSuccess, OnFailure const &onFailure) noexcept |
template<typename OnFailure , typename PromiseType = typename std::result_of<OnFailure(FailureType const&)>::type> | |
detail::GetPromiseType< PromiseType >::type | Catch (OnFailure const &onFailure) noexcept |
template<typename OnCompleted , typename PromiseType = typename std::result_of<OnCompleted()>::type> | |
detail::GetPromiseType< PromiseType >::type | Finally (OnCompleted const &onCompleted) noexcept |
Static Public Member Functions | |
static rili::Promise< void > | all (std::list< rili::Promise< void >> const &promises) noexcept |
static rili::Promise< void > | any (std::list< rili::Promise< void >> const &promises) noexcept |
static rili::Promise< std::list< std::exception_ptr > > | fetch (std::list< rili::Promise< void >> const &promises) noexcept |
Detailed Description
template<>
class rili::Promise< void >
rili::Promise API allow easily chaining asynchronous actions to avoid "callback hell".
- Remarks
- this is just specialization of rili::Promise<void> for void type.
- is copiable, but copy share state with original instance.
- is generally thread safe and instances can be pass throught and shared by multiple threads.
- all operations (even Construction) are made asynchronous way and are non blocking.
- Note
- rili::Promise<void> use rili::Context aquired during construction to dispatch asynchronously all further tasks(including chained children promises), so thread in which rili::Promise<void> is created may be important in some cases.
- rili::Promise<void> can be rejected or resolved only once - further reject or resolve will be skipped.
- Warning
- There cannot be rejected rili::Promise<void> which don't have (now or in future) rejection handlers, or rili::Promise<void>, which is not rejected nor resolved. Check for this is made in debug builds (assert is used) in rili::Promise<void> destructor.
Member Typedef Documentation
typedef std::function<void(OnResolveHandler const&)> rili::Promise< void >::BasicInitializer |
type of user callback for single parameter 'pending' rili::Promise<void> constructor
typedef std::function<void(OnResolveHandler const&, OnRejectHandler const&)> rili::Promise< void >::ComplexInitializer |
type of user callback for two parameter 'pending' rili::Promise<void> constructor
typedef std::exception_ptr rili::Promise< void >::FailureType |
FailureType is type in which all kinds of rejections / exceptions are stored in rili::Promise<void>
typedef std::function<void(FailureType const&)> rili::Promise< void >::OnRejectHandler |
can be used to reject rili::Promise<void>
typedef std::function<void()> rili::Promise< void >::OnResolveHandler |
can be used to resolve rili::Promise<void>
Constructor & Destructor Documentation
|
explicitnoexcept |
Create 'pending' rili::Promise<void>.
Call user provided function with callback as paramether which should be used to resolve rili::Promise<void>.
- Remarks
- User provided callback will be executed asynchronously
- is not blocking.
- is thread safe.
- Note
- If user provided function throw, then rili::Promise<void> is moved to rejected state with error equal to thrown exception.
- Warning
- If callback 'resolve' will not be called nor exception thrown then rili::Promise<void> will stay in 'pending' state infinitely which will block rili::Context::run infinitely.
- Parameters
-
initializer is function which is executed to resolve or reject rili::Promise<void>. It provide as parameter another function which can be used to resolve rili::Promise<void>.
Example:
|
explicitnoexcept |
Create 'pending' rili::Promise<void>.
Call user provided function with first parameter as callback which should be used to resolve rili::Promise<void> and second which should be used to reject it.
- Remarks
- User provided callback will be executed asynchronously
- is not blocking.
- is thread safe.
- Note
- If user provided function throw, then rili::Promise<void> is moved to rejected state with error equal to thrown exception.
- Warning
- If callback 'resolve' or 'reject' will not be called nor exception thrown, then rili::Promise<void> will stay in 'pending' state infinitely, which will block rili::Context::run infinitely.
- Parameters
-
initializer is function which is executed to resolve or reject rili::Promise<void>. It provide as first parameter another function which can be used to resolve rili::Promise<void> and as second function which can be used to reject rili::Promise<void>.
Example:
|
noexcept |
Create 'resolved' rili::Promise<void>.
- Remarks
- is not blocking.
- is thread safe.
Example:
|
explicitnoexcept |
Create 'rejected' rili::Promise<void> with rejected value.
- Parameters
-
rejected is error using which rili::Promise<void> should be constructed with 'rejected' state (without pending in meantime).
- Remarks
- is not blocking.
- is thread safe.
Example:
Member Function Documentation
|
staticnoexcept |
allow to transform collection of rili::Promise<void> into single, which will be rejected when ony of rili::Promise<void> in collection will be rejected.
In case, when all rili::Promise<void> in collection will be resolved, returned rili::Promise<void> also will be resolved.
- Remarks
- is not blocking
- is thread safe
- Parameters
-
promises - collection of rili::Promise<void> which should be transofrmed into single one
- Returns
- transformed rili::Promise<void>
- See also
- rili::Promise::all
- rili::Promise::any
- rili::Promise::fetch
- rili::Promise<void>::any
- rlii::Promise<void>::fetch
|
staticnoexcept |
allow to transform collection of rili::Promise<void> into single, which will be resolved when ony of rili::Promise<void> in collection will be resolved.
In case, when all rili::Promise<void> in collection will be rejected, returned rili::Promise<void> also will be rejected with rili::ComplexException which will hold all rejection results
- Remarks
- is not blocking
- is thread safe
- Parameters
-
promises - collection of rili::Promise<void> which should be transofrmed into single one
- Returns
- transformed rili::Promise<void>
- See also
- rili::Promise::all
- rili::Promise::any
- rili::Promise::fetch
- rili::Promise<void>::all
- rlii::Promise<void>::fetch
|
inlinenoexcept |
Allow to create chained rili::Promise for failure scenario.
It is used to branch execution path in case of failures.
- In case when current rili::Promise<void> will be rejected, then immediately and asynchronously onFailure will be called with current rili::Promise<void> 'rejected' error as parameter.
- In case when current rili::Promise<void> will be resolved, result will be moved to next chained rili::Promise<void>(onFailure will be skipped).
- Remarks
- is not blocking.
- is thread safe.
- if onFailure throw, then returned rili::Promise<void> will be automatically rejected with thrown exception as error.
- Parameters
-
onFailure callback will be called only when current rili::Promise<void> will be rejected. It can return next rili::Promise<void> or void type value.
- Returns
- new rili::Promise<void> chained to current one.
|
staticnoexcept |
allow to transform collection of rili::Promise<void> into single one, which will be resolved with collection of resolved/rejected values corresponding with given rili::Promise<void> from input collection.
- Note
- Result of returned rili::Promise will contain not initialized std::exception_ptr for corresponding rili::Promise from input collection which was resolved or initialized(by rejection value) for these which were rejected
- Remarks
- is not blocking
- is thread safe
- Parameters
-
promises - collection of rili::Promise<void> which should be transofrmed into single one
- Returns
- transformed rili::Promise
|
inlinenoexcept |
Allow to create chained rili::Promise for 'completion' scenario.
It is used when result of operation is not interesting - only it completion.
- In case when current rili::Promise<void> will be rejected, then immediately and asynchronously onCompleted will be called.
- In case when current rili::Promise<void> will be resolved, then immediately and asynchronously onCompleted will be called.
- Remarks
- is not blocking.
- is thread safe.
- if onCompleted throw, then returned rili::Promise will be automatically rejected with thrown exception as error.
- Parameters
-
onCompleted callback will be called when current rili::Promise<void> will be rejected or resolved. It can return next rili::Promise or other type value(which will be converted to resolved rili::Promise) inluding void type.
- Returns
- new rili::Promise chained to current one with type deduced based on onCompleted return type.
void rili::Promise< void >::reject | ( | FailureType const & | rejected | ) |
allow to reject rili::Promise<void> if not resolved/rejected yet
- Parameters
-
rejected value using which rili::Promise<void> should be rejected
void rili::Promise< void >::resolve | ( | ) |
allow to resolve rili::Promise<void> if not resolved/rejected yet
|
inlinenoexcept |
Allow to create chained rili::Promise for succesfull scenario.
- In case when current rili::Promise<void> will be resolved, then immediately and asynchronously onSuccess will be called.
- In case when current rili::Promise<void> will be rejected, then rili::Promise created by this method will be also rejected.
- Remarks
- is not blocking.
- is thread safe.
- if onSuccess throw, then returned rili::Promise will be automatically rejected with thrown exception as error.
- Parameters
-
onSuccess callback will be called only when current rili::Promise<void> will be resolved. It can return next rili::Promise or other type value(which will be converted to resolved rili::Promise) inluding void type.
- Returns
- new rili::Promise chained to current one with type deduced based on onSucces return type.
|
inlinenoexcept |
Allow to create chained rili::Promise for succesfull and Failure scenario.
Is used when branching further execution path is not needed and failure handling will be implemented in onFailure.
- In case when current rili::Promise<void> will be resolved, then immediately and asynchronously onSuccess will be called.
- In case when current rili::Promise<void> will be rejected, then immediately and asynchronously onFailure will be called with current rili::Promise<void> 'reject' error as parameter.
- Remarks
- is not blocking.
- is thread safe.
- if onSuccess or onFailure throw, then returned rili::Promise will be automatically rejected with thrown exception as error.
- return value from onSuccess and onFailure must be 'convertialbe' to the same rili::Promise type. Incorrect combinations will be detected in compile time.
Examples of correct combinations:
[]() -> A{...}, [](...) -> A{...}
,[]() -> A{...}, [](...) -> rili::Promise<A>{...}
,[]() -> rili::Promise<A>{...}, [](...) -> A{...}
,[]() -> rili::Promise<A>{...}, [](...) -> rili::Promise<A>{...}
,
Examples of incorrect combinations:
[]() -> A{...}, [](...) -> B{...}
,[]() -> A{...}, [](...) -> rili::Promise<B>{...}
,[]() -> rili::Promise<A>{...}, [](...) -> B{...}
,[]() -> rili::Promise<A>{...}, [](...) -> rili::Promise<B>{...}
,
- Parameters
-
onSuccess callback will be called only when current rili::Promise<void> will be resolved. It can return next rili::Promise or other type value(which will be converted to resolved rili::Promise) inluding void type. onFailure callback will be called only when current rili::Promise<void> will be rejected. It can return next rili::Promise or other type value(which will be converted to resolved rili::Promise) inluding void type.
- Returns
- new rili::Promise chained to current one with type deduced based on onSucces and onFailure return type.
The documentation for this class was generated from the following file:
- rili/Promise.hpp