node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

futurize

futurize

build status npm version codecov.io

Turn callback-style functions or promises into futures

-function read(path) {
-  return new Future(function(reject, resolve) {
-    fs.readFile(path, function(error, data) {
-      if (error)  reject(error)
-      else        resolve(data)
-    })
-  })
-}

+const read = futurize(Future)(fs.readFile);

Example

Futurize a callback-style function

import { futurize } from 'futurize';
import { Future } from 'ramda-fantasy';
// or
import Task from 'data.task';

const future = futurize(Future); // or futurize(Task);

import { readFile } from 'fs'

const read = future(readFile);

function decode(buffer) {
  return buffer.map(a => a.toString('utf-8'));
}

const readme = decode(read('README.md'));
const license = decode(read('LICENSE.md'));

const concatenated = readme.chain(a => license.map(b => a + b));

concatenated.fork(
  error => console.error(error)
, data => console.log(data)
);

Futurize a promise

import { futurizeP } from 'futurize';
import Task from 'data.task';
import myPromisedFunction from 'a-module';

const future = futurize(Future); // or futurize(Task);

const myFuturizedFunction = future(myPromisedFunction);

API

futurize :: Constructor -> CPS -> ( ...args -> Future )
futurizeP :: Constructor -> Promise -> ( ...args -> Future )

License

MIT © stoeffel