ResultPromise
public class ResultPromise<T, E> where E : Error
ResultPromise
is a minimal Promise
implementation that wraps a
Result<T, E>
instance. It supports the fulfil
, await
, then
and
thenAsync
combinators, facilitating a fluid sequencing of computations that
produce either a Result
(then
) or a ResultPromise
instance (thenAsync
).
(The reject
combinator is redundant, as failure is handled by the embedded
Result
.)
-
Fulfil this promise with
result
.Declaration
Swift
public func fulfil(with result: Result<T, E>)
Parameters
result
A
Result<T, E>
that describes the result. -
Execute the
observer
closure when the promise is fulfilled.Declaration
Swift
public func await(with observer: @escaping (Result<T, E>) -> Void)
Parameters
observer
A closure that takes a
Result
instance and returns nothing. -
Produce a new
ResultPromise
byflatMap
‘ing the supplied function over the embeddedResult
.Declaration
Swift
public func then<U>(_ f: (@escaping (T) -> Result<U, E>)) -> ResultPromise<U, E>
Parameters
f
A closure
(T) -> Result<U, E>
.Return Value
A new
ResultPromise<U, E>
instance, where the embeddedResult<U, E>
is the result offlatMap
‘ingf
over theResult<T, E>
embedded in this instance. -
Produce a new
ResultPromise
by applying the supplied function to the value contained in the embeddedResult
instance.Declaration
Swift
public func thenAsync<U>(_ f: (@escaping (T) -> ResultPromise<U, E>)) -> ResultPromise<U, E>
Parameters
f
A closure
(T) -> Result<U, E>
.Return Value
A new
ResultPromise<U, E>
instance.