Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

aulme/plift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Plift - use promises as values

Inspired by and intended to work well with Ramda.js.

Usage

A plifted function can be invoked with any combination of promises and values in its parameters. Plifted functions always return promises, even if all arguments are values.

const plift = require('plift');
const add = plift((a, b, c) => a + b + c);
const log = plift(console.log);

const sum = add(1, 2, 3);

sum.then(console.log); // 6
log(sum); // 6

const sum2 = add(Promise.resolve(1), 2, Promise.resolve(3));
log(sum2); // 6

Plifted functions are curried by default. If you don't know what currying is and why it is useful, here's an intro.

const add3 = add(1, 2);
const sum3 = add3(Promise.resolve(10));
log(sum3); // 13

This allows you to treat async values as first-class citizens instead of forcing your code into an endless pipe of thens.

const remoteValue = Promise.resolve(5);
const remoteValue2 = Promise.resolve(10);

const add = plift((a, b) => a + b);
const multiply = plift((a, b) => a * b);

const added = add(1, remoteValue);
const multiplied = multiply(added, remoteValue2);

log(multiplied);

You can plift entire objects, which recursively plifts all functions within that object.

const R = plift(require('ramda'));
const add5All = R.map(R.add(5));
const x = add5All(Promise.resolve([1, 2, 3]));
log(R.sum(x)); //21

For functional composition plift automatically flattens resulting promise structures, so R.map(R.add(5))([1, 2, 3]) returns a promise for P[6, 7, 8] rather then P[P6, P7, P8]. The same is true for object properties.

Installation

In terminal:

npm install plift

Then in code or in node repl:

const plift = require('plift');

License

MIT

About

Use promises as values

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published