Skip to content

Turn callback-style functions or promises into futures

License

Notifications You must be signed in to change notification settings

futurize/futurize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 callback with multiple arguments

import { futurizeV } from 'futurize';
import { Future } from 'ramda-fantasy';

const futureV = futurizeV(Future);

const read = futureV(fs.read);

read(fs.openSync('package.json', 'r'), new Buffer([]), 0, 2, 0)
.fork(console.error, ([bytesRead, buf]) => {
  console.log(buf.toString('utf8'));
});

Futurize a promise

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

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

const myFuturizedFunction = future(myPromisedFunction);

API

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

License

MIT © stoeffel

About

Turn callback-style functions or promises into futures

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •