npm install --save @lxsmnsyc/future
yarn add @lxsmnsyc/future
@lxsmnsyc/future attemps to extend the functionality and solve some problems that exists with the current ES6 Promise.
First difference about
Promises is that the computation is deferred: You can compose
Futures without evaluating/scheduling the computation already, which is good specially if you want to
compose your own logic, as well as, since
Promises' resolution callback executes synchronously, you can choose when will the
Future run its computation.
Futures are the cold version of
// No computation being processed;// Begin computation;// get the computed valuecomputation.then;// or;console.log'Success: ', value;
Another difference is that
Futures are cancellable. Disposing logic in
Promises has been a problem, and
Futures helps you with that. If a
Future is cancelled, the computation stops and the asynchronous value will not be resolved. If you start a
Future inside an async function, await its value, and cancels it before it has finished its computation, the async function will not continue executing.
// Begin computation;// get the computed valuecomputation.then;// cancel the computation before it finishescomputation.cancel;
Future.create is the basic operator for building your own computation logic for a
Example below is a
Future that resolves
Hello World after 500 ms.
// Create our Future;
Futures have two methods that allows you to transform the
Future into a another
Future with different computation logic:
compose which accepts a transformation function and
pipe which accepts multiple.
For example, if we want to modify the resolved value of a
Future, we can use
.composeFuture.map` World`;greeting.get.thenconsole.log; // Hello World
Computation vs Future
There are two core classes in this library: Futures and Computation.
To clarify, a Computation is an instance received by calling the
Future#get. A Future tells how a Computation instance should run its logic.
Full documentation is available here.
This project is bootstraped with TSDX with minor changes.
MIT © lxsmnsyc