rili::Promise< PromisedType > Class Template Reference
Inheritance diagram for rili::Promise< PromisedType >:
[legend]
Collaboration diagram for rili::Promise< PromisedType >:
[legend]

Public Types

typedef std::exception_ptr FailureType
 
typedef std::function< void(PromisedType const &)> 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 (PromisedType const &promised) noexcept
 
 Promise (FailureType const &rejected) noexcept
 
 Promise (BasicInitializer const &initializer) noexcept
 
 Promise (ComplexInitializer const &initializer) noexcept
 
void resolve (PromisedType const &resolved)
 
void reject (FailureType const &rejected)
 
template<typename OnSuccess , typename PromiseType = typename std::result_of<OnSuccess(PromisedType const&)>::type>
detail::GetPromiseType< PromiseType >::type Then (OnSuccess const &onSuccess) noexcept
 
template<typename OnSuccess , typename OnFailure , typename PromiseType1 = typename std::result_of<OnSuccess(PromisedType const&)>::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< std::list< PromisedType > > all (std::list< rili::Promise< PromisedType >> const &promises) noexcept
 
static rili::Promise< PromisedType > any (std::list< rili::Promise< PromisedType >> const &promises) noexcept
 
static rili::Promise< std::shared_ptr< std::list< std::pair< std::unique_ptr< PromisedType >, FailureType > > > > fetch (std::list< rili::Promise< PromisedType >> const &promises) noexcept
 

Detailed Description

template<typename PromisedType>
class rili::Promise< PromisedType >

rili::Promise API allow easily chaining asynchronous actions to avoid "callback hell".

Remarks
have specialization for void type: rili::Promise<void>
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
PromisedType must be copyable and copy assignable.
rili::Promise use rili::Context aquired during construction to dispatch asynchronously all further tasks(including chained children promises), so thread in which rili::Promise is created may be important in some cases.
rili::Promise can be rejected or resolved only once - further reject or resolve will be skipped.
Warning
There cannot be rejected rili::Promise which don't have (now or in future) rejection handlers, or rili::Promise, which is not rejected nor resolved. Check for this is made in debug builds (assert is used) in rili::Promise destructor.
See also
rili::Context
rili::Promise<void>
rili::CancelablePromise
rili::CancelablePromise<void>

Member Typedef Documentation

template<typename PromisedType >
typedef std::function<void(OnResolveHandler const&)> rili::Promise< PromisedType >::BasicInitializer

type of user callback for single parameter 'pending' rili::Promise constructor

template<typename PromisedType >
typedef std::function<void(OnResolveHandler const&, OnRejectHandler const&)> rili::Promise< PromisedType >::ComplexInitializer

type of user callback for two parameter 'pending' rili::Promise constructor

template<typename PromisedType >
typedef std::exception_ptr rili::Promise< PromisedType >::FailureType

is type in which all kinds of rejections / exceptions are stored in rili::Promise

template<typename PromisedType >
typedef std::function<void(FailureType const&)> rili::Promise< PromisedType >::OnRejectHandler

can be used to reject rili::Promise

template<typename PromisedType >
typedef std::function<void(PromisedType const&)> rili::Promise< PromisedType >::OnResolveHandler

can be used to resolve rili::Promise

Constructor & Destructor Documentation

template<typename PromisedType >
rili::Promise< PromisedType >::Promise ( PromisedType const &  promised)
inlineexplicitnoexcept

Create 'resolved' rili::Promise with promised value.

Parameters
promisedis value using which rili::Promise should be constructed with 'resolved' state (without pending in meantime).
Remarks
is not blocking.
is thread safe.

Example:

rili::Promise<int> promise(5);
template<typename PromisedType>
rili::Promise< PromisedType >::Promise ( FailureType const &  rejected)
inlineexplicitnoexcept

Create 'rejected' rili::Promise with rejected value.

Parameters
rejectedis error using which rili::Promise should be constructed with 'rejected' state (without pending in meantime).
Remarks
is not blocking.
is thread safe.

Example:

rili::Promise<int> promise(std::make_exception_ptr("Huston, we have a problem!!!"));
template<typename PromisedType >
rili::Promise< PromisedType >::Promise ( BasicInitializer const &  initializer)
explicitnoexcept

Create 'pending' rili::Promise.

Call user provided function with callback as paramether which should be used to resolve rili::Promise.

Remarks
User provided callback will be executed asynchronously
is not blocking.
is thread safe.
Note
If user provided function throw, then rili::Promise 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 will stay in 'pending' state infinitely which will block rili::Context::run infinitely.
Parameters
initializeris function which is executed to resolve or reject rili::Promise. It provide as parameter another function which can be used to resolve rili::Promise.

Example:

rili::Promise<int> promise([](auto const& resolve){ resolve(5); });
template<typename PromisedType >
rili::Promise< PromisedType >::Promise ( ComplexInitializer const &  initializer)
explicitnoexcept

Create 'pending' rili::Promise.

Call user provided function with first parameter as callback which should be used to resolve rili::Promise 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 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 will stay in 'pending' state infinitely, which will block rili::Context::run infinitely.
Parameters
initializeris function which is executed to resolve or reject rili::Promise. It provide as first parameter another function which can be used to resolve rili::Promise and as second function which can be used to reject rili::Promise.

Example:

rili::Promise<int> promise([](auto const&, auto const& reject){
reject(std::make_exception_ptr("Huston, we have a problem!!!"));
});

Member Function Documentation

template<typename PromisedType>
static rili::Promise<std::list<PromisedType> > rili::Promise< PromisedType >::all ( std::list< rili::Promise< PromisedType >> const &  promises)
inlinestaticnoexcept

allow to transform collection of rili::Promise into single, which will be rejected when ony of rili::Promise in collection will be rejected.

In case, when all rili::Promise in collection will be resolved, returned rili::Promise also will be resolved with collection of results of rili::Promise collection

Remarks
is not blocking
is thread safe
Parameters
promises- collection of rili::Promise which should be transofrmed into single one
Returns
transformed rili::Promise
See also
rili::Promise::any
rili::Promise::fetch
rili::Promise<void>::all
rili::Promise<void>::any
rlii::Promise<void>::fetch
template<typename PromisedType>
static rili::Promise<PromisedType> rili::Promise< PromisedType >::any ( std::list< rili::Promise< PromisedType >> const &  promises)
inlinestaticnoexcept

allow to transform collection of rili::Promise into single, which will be resolved when ony of rili::Promise in collection will be resolved.

In case, when all rili::Promise in collection will be rejected, returned rili::Promise 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 which should be transofrmed into single one
Returns
transformed rili::Promise
See also
rili::Promise::all
rili::Promise::fetch
rili::Promise<void>::all
rili::Promise<void>::any
rlii::Promise<void>::fetch
template<typename PromisedType>
template<typename OnFailure , typename PromiseType = typename std::result_of<OnFailure(FailureType const&)>::type>
detail::GetPromiseType<PromiseType>::type rili::Promise< PromisedType >::Catch ( OnFailure const &  onFailure)
inlinenoexcept

Allow to create chained rili::Promise for failure scenario.

It is used to branch execution path in case of failures.

Remarks
is not blocking.
is thread safe.
if onFailure throw, then returned rili::Promise will be automatically rejected with thrown exception as error.
Parameters
onFailurecallback will be called only when current rili::Promise will be rejected. It can return next rili::Promise or type value(which must be convertiable to current rili::Promise).
Returns
new rili::Promise chained to current one with type deduced based on current rili::Promise return type.
template<typename PromisedType>
static rili::Promise<std::shared_ptr<std::list<std::pair<std::unique_ptr<PromisedType>, FailureType> > > > rili::Promise< PromisedType >::fetch ( std::list< rili::Promise< PromisedType >> const &  promises)
inlinestaticnoexcept

allow to transform collection of rili::Promise into single one, which will be resolved with collection of resolved/rejected values corresponding with given rili::Promise from input collection.

Remarks
is not blocking
is thread safe
Parameters
promises- collection of rili::Promise which should be transofrmed into single one
Returns
transformed rili::Promise
See also
rili::Promise::all
rili::Promise::any
rili::Promise<void>::all
rili::Promise<void>::any
rlii::Promise<void>::fetch
template<typename PromisedType>
template<typename OnCompleted , typename PromiseType = typename std::result_of<OnCompleted()>::type>
detail::GetPromiseType<PromiseType>::type rili::Promise< PromisedType >::Finally ( OnCompleted const &  onCompleted)
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 will be rejected, then immediately and asynchronously onCompleted will be called.
  • In case when current rili::Promise 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
onCompletedcallback will be called when current rili::Promise 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.
template<typename PromisedType>
void rili::Promise< PromisedType >::reject ( FailureType const &  rejected)
inline

allow to reject rili::Promise if not resolved/rejected yet

Parameters
rejectedvalue using which rili::Promise should be rejected
template<typename PromisedType>
void rili::Promise< PromisedType >::resolve ( PromisedType const &  resolved)
inline

allow to resolve rili::Promise if not resolved/rejected yet

Parameters
resolvedvalue using which rili::Promise should be resolved
template<typename PromisedType>
template<typename OnSuccess , typename PromiseType = typename std::result_of<OnSuccess(PromisedType const&)>::type>
detail::GetPromiseType<PromiseType>::type rili::Promise< PromisedType >::Then ( OnSuccess const &  onSuccess)
inlinenoexcept

Allow to create chained rili::Promise for succesfull scenario.

  • In case when current rili::Promise will be resolved, then immediately and asynchronously onSuccess will be called with current rili::Promise 'resolve' result as parameter.
  • In case when current rili::Promise 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
onSuccesscallback will be called only when current rili::Promise 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.
template<typename PromisedType>
template<typename OnSuccess , typename OnFailure , typename PromiseType1 = typename std::result_of<OnSuccess(PromisedType const&)>::type, typename PromiseType2 = typename std::result_of<OnFailure(FailureType const&)>::type>
detail::GetPromiseType<PromiseType1>::type rili::Promise< PromisedType >::Then ( OnSuccess const &  onSuccess,
OnFailure const &  onFailure 
)
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 will be resolved, then immediately and asynchronously onSuccess will be called with current rili::Promise 'resolve' result as parameter.
  • In case when current rili::Promise will be rejected, then immediately and asynchronously onFailure will be called with current rili::Promise '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:

Examples of incorrect combinations:

Parameters
onSuccesscallback will be called only when current rili::Promise will be resolved. It can return next rili::Promise or other type value(which will be converted to resolved rili::Promise) inluding void type.
onFailurecallback will be called only when current rili::Promise 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: