Fluture
🦋 Fantasy Land compliant (monadic) alternative to Promises
Install / Use
/learn @fluture-js/FlutureREADME
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:
- Cancellation.
- Resource management utilities.
- Stack safe composition and recursion.
- Integration with [Sanctuary][S].
- A pleasant debugging experience.
For more information:
- API documentation
- [Article: Introduction to Fluture - A Functional Alternative to Promises][10]
- [Wiki: Compare Futures to Promises][wiki:promises]
- [Wiki: Compare Fluture to similar libraries][wiki:similar]
- [Video: Monad a Day - Futures by @DrBoolean][5]
Installation
With NPM
$ npm install --save fluture
Bundled from a CDN
To 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 Script: A JavaScript file that adds
Flutureto the global scope. Ideal for older browsers and code pens. - Fluture Script Minified: The same as above, but minified.
- Fluture Module: An EcmaScript module with named exports. Ideal for Deno or modern browsers.
- Fluture Module Minified: A minified EcmaScript module without TypeScript typings. Not recommended for Deno.
Usage
EcmaScript Module
Fluture is written as modular JavaScript.
- On Node 12 and up, Fluture can be loaded directly with
import 'fluture'. - On some older (minor) Node versions, you may need to import from
'fluture/index.js'instead, and/or pass--experimental-modulestonode. - On Node versions below 12, the [esm loader][esm] can be used. Alternatively, there is a CommonJS Module available.
- Modern browsers can run Fluture directly. If you'd like to try this out, I recommend installing Fluture with [Pika][] or [Snowpack][]. You can also try the bundled module to avoid a package manager.
- For older browsers, use a bundler such as [Rollup][] or WebPack. Besides the module system, Fluture uses purely ES5-compatible syntax, so the source does not have to be transpiled after bundling. Alternatively, there is a CommonJS Module available.
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))
CommonJS Module
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))
Documentation
Table of contents
<details open><summary>General</summary>- Installation instructions
- Usage instructions
- About the Fluture project
- On interoperability with other libraries
- How to read the type signatures
- How cancellation works
- On stack safety
- Debugging with Fluture
- Casting Futures to String
- Usage with Sanctuary
- Using multiple versions of Fluture alongside each other
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 function
attemptP: Create a Future using a Promise-returning functionencaseP: Convert a Promise-returning function to a Future functionpromise: Convert a Future to a Promise
pipe: 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 Futures
fork: Standard way to run a Future and get at its resultforkCatch: Fork with exception recoveryvalue: Shorter variant offorkfor Futures sure to succeeddone: Nodeback styleforkpromise: Convert a Future to a Promise
pap: 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 likeraceonConcurrentFutureinstances
pipe: 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 instance
Butterfly
The 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.
Interoperability
[<img src="https://raw.github.com/fantasyland/fantasy-land/m

