Chains this to target
such that any subsequent operations on
this future maker is reflected on target
.
const main = FutureMaker.empty<number>()
const child = FutureMaker.empty<number>()
// Now all operations on `child` will be redirected to `main`
child.chainTo(main)
// Completing `child` will complete `main`
child.complete(Success(1))
main.future() //=> Yields 1
child.future() //=> Yields 1
The purpose of this method is the same as with
ChainedCancelable, to be used in pieces of logic where
the chaining of onComplete
calls creates a memory leaks,
chaining being used to get rid of such chains.
This method is being used in the implementation of Future.flatMap for example to make it memory safe.
CREDITS: this was inspired by Scala's scala.concurrent.Scala
implementation.
Completes this FutureMaker
either with a successful value or
with a failure, but throws an exception if this maker was
already completed.
Due to throwing exceptions, this function is recommended for
usage in cases where there's a guarantee that the completion
of the FutureMaker
is attempted only once.
const m = FutureMaker.empty<number>()
m.complete(Success(1))
m.complete(Success(2)) //=> throws IllegalStateError
In case you have a concurrent race, see tryComplete for a version that does not throw exceptions.
Alias for complete(Failure(value))
.
See complete.
Creates and returns a Future that will complete when this future maker is completed.
const m = FutureMaker.empty<number>()
// Creates a simple future, no cancellation logic:
m.future()
// Creates a future with baked in cancellation logic:
const cRef = Cancelable.of(() => console.log("Cancelled!"))
m.future(cRef)
is an optional reference that can indicate cancellation logic to be baked into the created future
Alias for complete(Success(value))
.
See complete.
Tries to complete this future builder either with a successful value or with a failure.
This function can be used in concurrent races where multiple
actors compete for completing the same FutureMaker
.
const m = FutureMaker.empty<number>()
m.tryComplete(Success(1)) //=> true
m.tryComplete(Success(2)) //=> false
m.future() //=> Yields 1
In case you have a guarantee that the completion only happens once, then usage of complete is recommended.
false
in case the FutureMaker
has been already
completed, or true
otherwise
Alias for tryComplete(Failure(error))
.
See tryComplete.
Alias for tryComplete(Success(value))
.
See tryComplete.
Returns a new FutureMaker
that mirrors the state of the source,
but that uses the given Scheduler reference for
managing the required async boundaries.
The given Scheduler
reference is used for inserting async
boundaries when the registered listeners are triggered when
.complete is called or for data transformations
executed on the future references returned by
.future.
See Future.withScheduler.
Returns an already completed FutureMaker reference.
Example:
const m = FutureMaker.completed(Success(1))
m.future() // Yields 1
m.complete(Success(2)) // Throws IllegalStateError
If all you need is a Future
, then use Future.fromTry
instead.
Returns an empty FutureMaker
reference awaiting completion.
This is the builder that one should use for building
FutureMaker
instances, since the default constructor is not
exposed due to it exposing internal state.
Generated using TypeDoc
A write interface for Future to use when implementing producers.
This would be the equivalent of the now deprecated
Deferred
data type in JavaScript.Example:
import { Future, FutureMaker, Scheduler, Success } from "funfix" const ec = Scheduler.global.get() const m = FutureMaker.empty<number>() // The producer ec.scheduleOnce(1000, () => m.complete(Success(1))) // The future that will eventually complete when // `m.complete` gets called const f: Future<number> = maker.future()