Fantasy Land compliant (monadic) alternative to Promises
Fantasy Land compliant (monadic) alternative to Promises
Fluture offers a control structure similar to Promises, Tasks, Deferreds, and what-have-you. Let's call them Futures.
Much like Promises, Futures represent the value arising from the success or failure of an asynchronous operation (I/O). Though unlike Promises, Futures are lazy and adhere to the monadic interface.
Some of the features provided by Fluture include:
For more information:
$ npm install --save flutureTo load Fluture directly into a browser, a code pen, or [Deno][], use one of the following downloads from the JSDelivr content delivery network. These are single files that come with all of Fluture's dependencies pre-bundled.
Fluture to the global
scope. Ideal for older browsers and code pens.Fluture is written as modular JavaScript.
import 'fluture'.'fluture/index.js' instead, and/or pass --experimental-modules to node.import {readFile} from 'fs'
import {node, encase, chain, map, fork} from 'fluture'
const getPackageName = file => (
node (done => { readFile (file, 'utf8', done) })
.pipe (chain (encase (JSON.parse)))
.pipe (map (x => x.name))
)
getPackageName ('package.json')
.pipe (fork (console.error) (console.log))Although the Fluture source uses the EcmaScript module system,
the main file points to a CommonJS version of Fluture.
On older environments one or more of the following functions may need to be
polyfilled: [Object.create][JS:Object.create],
[Object.assign][JS:Object.assign] and [Array.isArray][JS:Array.isArray].
const fs = require ('fs')
const Future = require ('fluture')
const getPackageName = function (file) {
return Future.node (function (done) { fs.readFile (file, 'utf8', done) })
.pipe (Future.chain (Future.encase (JSON.parse)))
.pipe (Future.map (function (x) { return x.name }))
}
getPackageName ('package.json')
.pipe (Future.fork (console.error) (console.log))Future: Create a possibly cancellable Futureresolve: Create a resolved Futurereject: Create a rejected Futureafter: Create a Future that resolves after a timeoutrejectAfter: Create a Future that rejects after a timeoutgo: Create a "coroutine" using a generator functionattempt: Create a Future using a possibly throwing functionattemptP: Create a Future using a Promise-returning functionnode: Create a Future using a Node-style callbackencase: Convert a possibly throwing function to a Future functionencaseP: Convert a Promise-returning function to a Future functionattemptP: Create a Future using a Promise-returning functionencaseP: Convert a Promise-returning function to a Future functionpromise: Convert a Future to a Promisepipe: Apply a function to a Future in a fluent method chainmap: Synchronously process the success value in a Futurebimap: Synchronously process the success or failure value in a Futurechain: Asynchronously process the success value in a Futurebichain: Asynchronously process the success or failure value in a Futureswap: Swap the success with the failure valuemapRej: Synchronously process the failure value in a FuturechainRej: Asynchronously process the failure value in a Futurecoalesce: Coerce success and failure values into the same success valueap: Combine the success values of multiple Futures using a functionpap: Combine the success values of multiple Futures in parallel using a functionand: Logical and for Futuresalt: Logical or for Futureslastly: Run a Future after the previous settlesrace: Race two Futures against each otherboth: Await both success values from two Futuresparallel: Await all success values from many Futuresfork: Standard way to run a Future and get at its resultforkCatch: Fork with exception recoveryvalue: Shorter variant of fork for Futures sure to succeeddone: Nodeback style forkpromise: Convert a Future to a Promisepap: Combine the success values of multiple Futures in parallel using a functionrace: Race two Futures against each otherboth: Await both success values from two Futuresparallel: Await all success values from many FuturesConcurrentFuture: A separate data-type for doing algebraic concurrencyalt: Behaves like race on ConcurrentFuture instancespipe: Apply a function to a Future in a fluent method chaincache: Cache a Future so that it can be forked multiple timesisFuture: Determine whether a value is a Fluture compatible Futurenever: A Future that never settlesdebugMode: Configure Fluture's debug modecontext: The debugging context of a Future instanceThe name "Fluture" is a conjunction of "FL" (the acronym to [Fantasy Land][FL]) and "future". Fluture means butterfly in Romanian: A creature one might expect to see in Fantasy Land.
Credit goes to Erik Fuente for styling the logo, and [WEAREREASONABLEPEOPLE][9] for sponsoring the project.
[][FL]
Future implements [Fantasy Land][FL] 1.0+ -compatible
Alt, Bifunctor, Monad, and ChainRec
(of, ap, alt, map, bimap, chain, chainRec).Future.Par implements [Fantasy Land 3][FL] -compatible
Alternative (of, zero, map, ap, alt).@@type properties
for [Sanctuary Type Identifiers][STI].@@show properties for
[Sanctuary Show][SS].The various function signatures are provided in a small language referred to as Hindley-Milner notation.
In summary, the syntax is as follows: InputType -> OutputType. Now,
because functions in Fluture are [curried][Guide:currying], the "output" of a
function is often another function. In Hindley-Milner that's simply written
as InputType -> InputToSecondFunction -> OutputType and so forth.
By convention, types starting with an upper-case letter are
concrete types. When they start with a lower-case letter they're
type variables. You can think of these type variables as generic types.
So a -> b denotes a function from generic type a to generic type b.
Finally, through so-called constraints, type variables can
be forced to conform to an "interface" (or Type Class in functional jargon).
For example, MyInterface a => a -> b, denotes a function from generic type
a to generic type b, where a must implement MyInterface.
You can read in depth about [Hindley-Milner in JavaScript][Guide:HM] here.
The concrete types you will encounter throughout this documentation:
Future.Par).a and a resolution value of type b.No open issues yet, or sync has not completed.