Promise

trait Promise[+A]

Value Promise

Promise represents value which will be available later (at some point of time), but it can now be incorporated in current calculations

val s: String = "Hello"

val v: Promise[String] = Promise(s + " Concurrent Promise!")

v.resultOpt.tp    // Value is likely not available yet

v.onResult(_.tp) // Will run when value is available

// Output
Opt(VOID)
Result(Hello Concurrent Promise!)
Source
__.scala
class java.lang.Object
trait scala.Matchable
class Any

Def

def await(v: Time.Length)(using c: Promise.Context): Result[A]
Source
__.scala
def drop(f: A => Boolean, p: A => Result.Problem)(using c: Promise.Context): Promise[A]

Reversed filter

Reversed filter

Creates a new promise where in case if future value will satisfy given predicate, it will be replaced with generated problem result

The result with problem will not be affected

Source
__.scala
def fornil[U](p: Result.Problem => U)(using c: Promise.Context): Promise[A]

Process failure

Process failure

Will run the given function, when promise is fulfilled and the result is a problem

Note. The function will never run if promise is a success

Source
__.scala
def forval[U](f: A => U)(using c: Promise.Context): Promise[A]

Process value

Process value

Will run the given function, when value will become available

Note. The function will never run if promise fails

Source
__.scala
def map[B](f: A => B)(using c: Promise.Context): Promise[B]

Convert value

Convert value

Creates a new promise where future value will be converted with given function

The result with problem will not be affected

Source
__.scala
def mapAll[B](f: Result[A] => Result[B])(using c: Promise.Context): Promise[B]

Change completly

Change completly

Creates a new promise where all outcomes of current promise, both: value and problem will be processed

Source
__.scala
def mapResult[B](f: A => Result[B])(using c: Promise.Context): Promise[B]

Map result

Map result

Creates a new promise where future value will be converted to given function result

Source
__.scala
def mapWith[B](f: A => Promise[B])(using c: Promise.Context): Promise[B]

Map with Promise

Map with Promise

Creates a new promise, which will be completed based on promise created with the given function result.

Source
__.scala
def onResult[U](f: Result[A] => U)(using c: Promise.Context): Unit

Run when ready

Run when ready

Will run the given function, when the result will become available

val v = Promise{ 2 * 2 }

v.onResult("onResult: " + _.tag tp())

"Immediate Result: " + v.resultOpt tp()

 // Output
Immediate Result: Opt(VOID)
onResult: Result(4)
Source
__.scala
def process[U, W](f: A => U, p: Result.Problem => W)(using c: Promise.Context): Promise[A]

Process result

Process result

When promise is fulfilled with a result, the apropriate given function is executed either with value or problem

Source
__.scala
def resultOpt: Opt[Result[A]]

Optional result

Optional result

Returns calculation result if it is available

 val v = Promise{ 2 * 2 }

 v.resultOpt.tp        // Likely prints: \/

 J.sleep(1.Second)

 v.resultOpt.tp        // Likely prints: Opt(Result(4))
Source
__.scala
def take(f: A => Boolean, p: A => Result.Problem)(using c: Promise.Context): Promise[A]

Filter

Filter

Creates a new promise where in case if future value will not satisfy given predicate, it will be replaced with generated problem result

The result with problem will not be affected

Source
__.scala
def zip[B](v: Promise[B])(using c: Promise.Context): Promise[(A, B)]

Join two promises

Join two promises

Creates a new promise where values of two promises will be returned as tuple. If either promise fails, the new promise will also be a failure

Source
__.scala