Giter Club home page Giter Club logo

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

futurize's Projects

futurize icon futurize

Turn callback-style functions or promises into futures

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.