Schedules an Action and executes it thereafter.
Schedules an Action with a given time delay and executes it thereafter.
Resolves the Computation instance to the fastest given Future.
Example below resolves the amb
instance to B as it its faster than A.
const A = Future.timer('Hello', 500);
const B = Future.timer('World', 400);
Future.amb([A, B])
.get()
.then(console.log); // World
an Array of Future instances
Creates a Future with a given callback.
The callback receives a FutureEmitter instance which you can use to resolve/reject values as well as attach cleanup logic when a Computation gets cancelled.
// Create a Future that resolves 'Hello' after 500ms
const delayedHello = Future.create((emitter) => {
// Our timeout that resolves the value
const timeout = setTimeout(() => {
emitter.success('Hello')
}, 500);
// Attach cleanup
emitter.onCancel(() => {
clearTimeout(timeout);
});
});
type of the computed value
a function that receives a FutureEmitter
Runs an async function, resolving with the returned Promise's resolved value.
When the function is called, it receives two values:
const delayedHelloWorld = Future.fromAsync(async (onCancel, protect) => {
// Construct a delayed promise
const delayed = new Promise((resolve, reject) => {
// create the timeout
const timeout = setTimeout(() => {
resolve('Hello');
}, 500);
// register cancellation
onCancel(() => {
clearTimeout(timeout);
});
});
// get the value
const value = await protect(delayed); // do not resolve if cancelled.
// return the value
return `${value} World`;
});
type of the value to be returned.
Runs the Computation instances from the given array of Future instances at the same time, resolving after all Computation has resolved, ignoring their values.
const A = Future.timer('Hello', 500);
const B = Future.timer('World', 400);
Future.merge([A, B])
.get()
.then(console.log); // Resolves with an empty value after 500ms
type of the Future array
Returns a Computation that never rejects nor resolves.
const never = Future.never();
Resolves with the given value
Future.success('Hello World')
.get()
.then(console.log); // Hello World
type of the given value
Resolves a given value after a certain amount of time.
// a Future that resolves 'Hello' after 500ms
const delayedHello = Future.timer('Hello', 500);
delayedHello.get()
.then(console.log);
the type of the computed value
the value to be resolved into
the amount of delay in milliseconds
the function to schedule the timeout
Combines and resolves the outputs of the given array of Future computations.
const A = Future.timer('Hello', 500);
const B = Future.timer('World', 250);
Future.zip([A, B], (a, b) => `${a} ${b}`)
.get()
.then(console.log); // 'Hello World'
type of computed values of the Future instances.
type of the combination value.
an array of Future instances.
a function that receives the output of the Future instances and outputs a combination.
Schedules an Action using requestAnimationFrame
and executes thereafter.
Schedules an Action with requestAnimationFrame
and executes after a given time delay.
Schedules an Action using setImmediate
and executes thereafter.
Schedules an Action with setImmediate
and executes after a given time delay.
Schedules an Action asynchronously and executes thereafter.
Schedules an Action asynchronously and executes after a given time delay.
Schedules an Action synchronously and executes thereafter.
Schedules an Action synchronously and executes after a given time delay.
Schedules an Action using setTimeout
and executes thereafter.
Schedules an Action with setTimeout
and executes after a given time delay.
Resolves the Computation instance to the fastest given Future.
Example below resolves the ambWith
instance to B as it its faster than A.
const A = Future.timer('Hello', 500);
const B = Future.timer('World', 400);
A.compose(Future.ambWith(B))
.get()
.then(console.log); // World
Type of the computed value
Computes and ignores the composed Future instance and then resolves to the given Future instance after the computation.
Example below resolves the amb
instance to B as it its faster than A.
const A = Future.timer('Hello', 500);
const B = Future.timer('World', 400);
A.compose(Future.andThen(B))
.get()
.then(console.log); // World after 900s
computed value for the given Future instance
Computes and ignores the composed Future instance and then resolves to the given PromiseLike instance after the computation.
Example below resolves the amb
instance to B as it its faster than A.
const A = Future.timer('Hello', 500);
const B = Future.timer('World', 400);
A.compose(Future.andThen(B))
.get()
.then(console.log); // World after 900s
computed value for the given Future instance
Caches the initial computed value for the Future instance.
// Create a Future
const delayedHello = Future.timer('Hello', 500);
// Transform into a cached Future
const cached = delayedHello.compose(Future.cache());
// Perform the initial computation
cached.get().then(console.log);
// Try performing another computation
setTimeout(() => {
cached.get().then(console.log); // resolves without delay
}, 550);
Schedules the Computation instance on the given scheduler function.
Future.success('Hello')
.compose(Future.computeOn(Future.Schedulers.TIMEOUT.NOW))
.get();
.then(console.log);
type of the computed value
where to perform the computation
Concatenates the Computation instance provided by an array of Future instances into a single Computation, and resolves to last concatenated instance.
const A = Future.timer('Hello', 500);
const B = Future.timer('World', 400);
Future.concat([A, B])
.get()
.then(console.log); // World after 900s
Compares the resolved value with the given value and comparer and resolves with the comparer result.
Default comparer is the Object.is function.
type of the computed value
type of the value to be compared with.
the value to be compared with the resolved value.
a function that compares two values.
Resolves the Computation instance with the given value if the Computation resolves with a null/undefined value.
Future.success()
.compose(Future.defaultIfEmpty('Hello'))
.get()
.then(console.log); // Hello
type of computed and given value
a value to be resolved with
Delays the Computation instance's value by a certain amount of time.
type of the computed value
the amount of time to delay
where to schedule the time delay
immediately reject an error
Runs a side-effect with the given Action when a Computation instance resolves, rejects or gets cancelled.
type of the computed value
Runs a side-effect with the given Action when the Computation instance gets cancelled.
type of the computed value
Registers a Consumer2 that receives the resolved value on the first parameter or the rejected value on the second value
type of the computed value.
Registers a Consumer that receives the rejected value.
type of the computed value.
Registers a Consumer that receives the computed value.
type of the computed value and the value to be received by the given consumer.
Registers an Action that is called whenever a Computation resolves or rejects.
type of the computed value
Resolves the given Future if the resolved value passes the given predicate.
Transforms the resolved value by passing it through a transformer function that returns a Future instance.
Future.success('Hello World')
.compose(Future.flatMap(value => Future.success(value)))
.get()
.then(console.log); // Hello World
type of the value to be received by the transformer.
type of the valeu to be returned by the transformer.
Ignores the resolved value by resolving with an empty value.
Future.timer('Hello', 500)
.compose(Future.ignoreElement())
.get()
.then(console.log) // undefined
Transforms the resolved value of the given Future.
const A = Future.timer('Hello', 500);
const greeting = A.compose(Future.map(value => `${value} World`));
greeting.get().then(console.log); // Hello World
type of the received computed value
type of the resulting computed value from the transformer function
a function that transforms the resolved value
Runs the given Future and this Future's Computation instances at the same time, resolving after both Computation has resolved, ignoring their values.
const A = Future.timer('Hello', 500);
const B = Future.timer('World', 400);
A.compose(Future.mergeWith(B))
.get()
.then(console.log); // Resolves with an empty value after 500ms
Observes the resolved/rejected values of the Computation on a given Scheduler
Future.success('Hello World')
.compose(Future.observeOn(Future.Schedulers.SYNC.NOW))
.get()
.then(console.log);
type of the computed value
Where to observe the values
Resolves a rejected Computation by shifting towards the computation of the returned Future from the given function.
Future.failure(new Error('Bad'))
.compose(Future.onErrorResume(() => Future.success('Hello World')))
.get()
.then(console.log);
type of the computed value and resumed function result
a function which accepts the rejected Error and returns a Future to shift with.
Resolves a rejected Computation by shifting towards the computation of the given Future.
Future.failure(new Error('Bad'))
.compose(Future.onErrorResumeNext(Future.success('Hello World')))
.get()
.then(console.log);
type of the computed value and resumed function result
Resolves a rejected Computation with the returned value from a given function.
Future.failure(new Error('Bad'))
.compose(Future.onErrorReturn(() => 'Hello World'))
.get()
.then(console.log);
type of the computed value and resumed function result
a function which accepts the rejected Error
Resolves a rejected Computation with the given item
Future.failure(new Error('Bad'))
.compose(Future.onErrorReturnItem('Hello World'))
.get()
.then(console.log);
type of the computed and given item
item to resolve with after rejection
Retries a rejected Computation until it resolves.
proneToErrorFuture.compose(Future.retry());
type of the computed value
Retries the rejected Computation rejecting with the recent error if it fails to resolve with the given amount of retries.
proneToErrorFuture.compose(Future.retryCounted(5));
the type of the computed value
the amount of retries
Retries a rejected Computation until a certain amount of time or, if provided, the given predicate returns true, rejecting with the recent error.
proneToErrorFuture.compose(Future.retryTimed(500));
type of the computed value
The amount of time
Where to schedule the timeout
a predicate which receives the current rejected error.
Retries a rejected Computation until the given Predicate returns true, rejecting with the recent error.
proneToErrorFuture.compose(Future.retryUntil(processError));
type of the computed value
a predicate which receives the current rejected error.
Schedules the Computation instance's subscription with the given scheduler.
Similar to computeOn, except that the Computation in subscribeOn
begins synchronously.
Future.success('Hello')
.compose(Future.subscribeOn(Future.Schedulers.TIMEOUT.NOW))
.get();
.then(console.log);
type of the computed value
where to perform the computation
Resolves the Computation instance with the given value if the Computation resolves with a null/undefined value.
Future.success()
.compose(Future.defaultIfEmpty('Hello'))
.get()
.then(console.log); // Hello
type of computed and given value
Rejects with TimeoutError when this Future fails to resolve within a certain amount of time.
// A Future that resolves after 500ms
const delayedHello = Future.timer('Hello', 500);
// Compose a new Future which only allows resolution within 400ms
const strict = delayedHello.compose(Future.timeout(400));
// Begin the computation and expect an error
strict.get()
.catch(console.log);
the amount of time
where to schedule the timeout
const A = Future.timer('Hello', 500);
const B = Future.timer('World', 250);
A.compose(Future.zipWith(B, (a, b) => `${a} ${b}`))
.get()
.then(console.log); // 'Hello World'
type of computed value for this Future.
type of computed value for the other Future.
type of the combination value.
a Future instance.
a function that receives the output of the two Future instances and outputs a combination.
Generated using TypeDoc
A type annotation for a function that transforms a Future instance into another Future instance.
Used for Future.compose and Future.pipe.