Creates a Future from a function returning a Promise, which can be inline in the call, for instance:
const f1 = Future.ok(1);
const f2 = Future.ok(2);
return Future.do(async () => {
const v1 = await f1;
const v2 = await f2;
return v1 + v2;
});
Build a failed Future with the error data you provide.
Returns a Future
that'll complete when the first Future
of
the iterable you give will complete, with the value of that first
future. Be careful, completing doesn't necessarily mean completing
successfully!
Also see Future.firstSuccessfulOf
Returns a Future
that'll complete when the first Future
of
the iterable you give will complete successfully, with the value of that first
future.
Also see Future.firstCompletedOf
Applicative lifting for Future. Takes a function which operates on basic values, and turns it in a function that operates on futures of these values ('lifts' the function). The 2 is because it works on functions taking two parameters.
the first future type
the second future type
the new future type as returned by the combining function.
Applicative lifting for Future. 'p' stands for 'properties'.
Takes a function which operates on a simple JS object, and turns it in a function that operates on the same JS object type except which each field wrapped in a Future ('lifts' the function). It's an alternative to Future.liftA2 when the number of parameters is not two.
the object property type specifying the parameters for your function
the type returned by your function, returned wrapped in a future by liftAp.
Build a Future from an existing javascript Promise.
Build a Future from a node-style callback API, for instance:
Future.ofCallback<string>(cb => fs.readFile('/etc/passwd', 'utf-8', cb))
Build a Future in the same way as the 'new Promise' constructor. You get one callback to signal success (resolve), failure (reject), or you can throw to signal failure.
Future.ofPromiseCtor<string>((resolve,reject) => setTimeout(resolve, 10, "hello!"))
Build a successful Future with the value you provide.
Turns a list of futures in a future containing a list of items. Useful in many contexts.
But if a single future is failed, you get back a failed Future.
Also see Future.traverse
Takes a list, a function that can transform list elements to futures, then return a Future containing a list of the transformed elements.
But if a single element results in failure, the result also resolves to a failure.
There is an optional third parameter to specify options.
You can specify {maxConcurrent: number}
to request that
the futures are not all triggered at the same time, but
rather only 'number' at a time.
Also see Future.sequence
Has no effect on a failed Future. If the Future was successful, will check whether its value matches the predicate you give as first parameter. If the value matches the predicate, an equivalent Future to the input one is returned.
If the value doesn't match predicate however, the second parameter function is used to compute the contents of a failed Future that'll be returned.
Transform the value contained in a successful Future. You return a
Future, but it is then "flattened" so we still return a Future
Transform the value contained in a successful Future. Has no effect if the Future was failed. Will turn a successful Future in a failed one if you throw an exception in the map callback (but please don't do it.. Rather use Future.filter or another mechanism).
Transform the value contained in a failed Future. Has no effect if the Future was successful.
Execute the side-effecting function you give if the Future is a failure.
The Future is unchanged by this call.
Execute the side-effecting function you give if the Future is a success.
The Future is unchanged by this call.
Has no effect if this Future is successful. If it's failed however, the function you give will be called, receiving as parameter the error contents, and a Future equivalent to the one your function returns will be returned.
The then
call is not meant to be a part of the Future
API,
we need then so that await
works directly.
Please rather use Future.map or Future.flatMap.
Get a Promise
from this Future
.
Transform this value to another value type. Enables fluent-style programming by chaining calls.
Generated using TypeDoc
A Future is the equivalent, and ultimately wraps, a javascript Promise. While Futures support the Future.then call (so that among others you can use
await
on them), you should call Future.map and Future.flatMap.Futures represent an asynchronous computation. A Future will only ever be computed once at most. Once it's computed, calling Future.map or
await
will return instantly.