abstract Promise<T>(Surprise<T, Error>)
package tink.core
from Surprise<T, Error>, to Surprise<T, Error>,
Representation of the outcome of a potentially asynchronous operation that can fail.
This type is a compile-time wrapper over Future<Outcome<T, tink.core.Error>>
that provides
convenience API for dealing with failure outcomes.
Static variables
Static methods
staticiterate<A, R>(promises:Iterable<Promise<A>>, yield:Next<A, Option<R>>, fallback:Promise<R>):Promise<R>
Given an Iterable (e.g. Array) of Promises, handle them one by one with the yield
function until one of them yields Some
value
and the returned promise will resolve that value. If all of them yields None
, the returned promise will resolve to the fallback
promise.
In a nutshell, it is the async version of the following code:
for(promise in promises) {
switch yield(promise) {
case Some(v): return v;
case None:
}
}
return fallback;
Parameters:
promises | An Iterable (e.g. Array) of Promises |
---|---|
yield | A function used to handle the promises and should return an Option |
fallback | A value to be used when all yields |
Returns:
Promise
staticmerge<A, R>(this:Surprise<T, Error>, other:Promise<A>, merger:Combiner<T, A, R>, ?gather:Gather):Promise<R>
staticretry<T>(gen:() ‑> Promise<T>, next:Next<{error:Error, elapsed:Float, attempt:Int}, Noise>):Promise<T>
Retry a promise generator repeatedly
Parameters:
gen | A function that returns a |
---|---|
next | A callback to be called when an attempt failed. An object will be received containing the info of the last attempt:
If this function's returned promised resolves to an Some usage examples:
- wait longer for later attempts and stop after a limit:
}
|
Returns:
Promise