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

@:value(cast Future.NEVER)staticfinalread onlyNEVER:Promise<Never> = cast Future.NEVER

@:value(Future.sync(Success(Noise)))staticfinalread onlyNOISE:Promise<Noise> = Future.sync(Success(Noise))

@:value(NOISE)staticfinalread onlyNULL:Promise<Noise> = NOISE

Deprecated: "use Promise.NOISE instead"

Static methods

@:noUsingstaticcache<T>(gen:() ‑> Promise<Pair<T, Future<Noise>>>):() ‑> Promise<T>

staticinlineeager(this:Surprise<T, Error>):Promise<T>

staticinlineflatMap<R>(this:Surprise<T, Error>, f:Outcome<T, Error> ‑> Future<R>):Future<R>

staticinlinehandle(this:Surprise<T, Error>, cb:Callback<Outcome<T, Error>>):CallbackLink

@:noUsingstaticinParallel<T>(a:Array<Promise<T>>, ?concurrency:Int):Promise<Array<T>>

@:noUsingstaticinSequence<T>(a:Array<Promise<T>>):Promise<Array<T>>

@:noUsingstaticiterate<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 None

Returns:

Promise

@:noUsingstaticinlinelazy<T>(p:Lazy<Promise<T>>):Promise<T>

@:noUsingstaticinlinelift<T>(p:Promise<T>):Promise<T>

staticinlinemap<R>(this:Surprise<T, Error>, f:Outcome<T, Error> ‑> R):Future<R>

staticmapError(this:Surprise<T, Error>, f:Error ‑> Error):Promise<T>

staticmerge<A, R>(this:Surprise<T, Error>, other:Promise<A>, merger:Combiner<T, A, R>, ?gather:Gather):Promise<R>

staticnext<R>(this:Surprise<T, Error>, f:Next<T, R>, ?gather:Gather):Promise<R>

@:tostaticnoise(this:Surprise<T, Error>):Promise<Noise>

staticinlinerecover(this:Surprise<T, Error>, f:Recover<T>):Future<T>

@:noUsingstaticinlinereject<A>(e:Error):Promise<A>

@:noUsingstaticinlineresolve<A>(v:A):Promise<A>

@:noUsingstaticretry<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 Promise, this function will be called multiple times during the retry process

next

A callback to be called when an attempt failed. An object will be received containing the info of the last attempt: attempt is the number of attempts tried, starting from 1 error is the error produced from the last attempt elasped is the amount of time (in ms) elapsed since the beginning of the retry call

If this function's returned promised resolves to an Error, this retry will abort with such error. Otherwise if it resolves to a Success(Noise), the retry will continue.

Some usage examples: - wait longer for later attempts and stop after a limit: `haxe function (info) return switch info.attempt {

  case 10: info.error;
  case v: Future.delay(v * 1000, Noise);

} `

  • bail out on error codes that are fatal: haxe function (info) return switch info.error.code { case Forbidden : info.error; // in this case new attempts probably make no sense default: Future.delay(1000, Noise); }

  • and also actually timeout: haxe // with using DateTools function (info) return if (info.elapsed > 2.minutes()) info.error else Future.delay(1000, Noise);

Returns:

Promise

staticinlineswap<R>(this:Surprise<T, Error>, v:R):Promise<R>

staticinlineswapError(this:Surprise<T, Error>, e:Error):Promise<T>

@:noUsingstaticinlinetrigger<A>():PromiseTrigger<A>

Creates a new PromiseTrigger

staticinlinetryRecover(this:Surprise<T, Error>, f:Next<Error, T>):Promise<T>