Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pacta

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pacta

An algebraic implementation of Promises.

  • 0.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.7K
increased by6595%
Maintainers
1
Weekly downloads
 
Created
Source

pacta Build Status

This is an implementation of algebraic Promises in node.js having been convinced by James Coglan and Aanand Prasad.

Pacta's promises can be used as the following algebraic structures:

  • Semigroups (through Promise#concat which concatenates promises containing semigroups such as arrays and strings);
  • Monoids (through Promise#empty which returns an empty version of a promise that contains a monoid);
  • Functors (through Promise#map);
  • Applicative (through Promise#ap and Promise.of);
  • Chains (through Promise#chain);
  • Monads (through all of the above).

Above that, Pacta also provides:

  • conjoin to concatenate promises into a list of values regardless of their original type meaning that non-Monoid types can be combined with others (e.g. a promise of 'foo' can be conjoined with [1, 2] to produce ['foo', 1, 2]);
  • combine to conjoin promises without flattening lists (e.g. combining a promise of [1, 2] and [3] will give a promise of [[1, 2], [3]] instead of [1, 2, 3] as it would with concat and conjoin);
  • explode to map over a promise's value but, instead of receiving a single value, explode the promise's value into seperate arguments:
Promise.of([1, 2]).explode(function (x, y) {
  console.log(x); //=> 1
  console.log(y); //=> 2
});

It also defines a monoid interface for Array and String, implementing empty such that:

Array.empty();  //=> []
String.empty(); //=> ""

Note that Pacta does not handle errors or the concept of a failed promise as yet.

See the HTTP client example and the test suite for more information.

Usage

var Promise = require('pacta').Promise;

var p = new Promise();
setTimeout(function () {
  p.resolve('Foo');
}, 1000);

p.map(console.log); //=> "Foo"

p.map(function (x) {
  return x + '!';
}).map(console.log); //=> "Foo!"

var p2 = new Promise();
setTimeout(function () {
  p2.resolve(['bar']);
}, 500);

var p3 = Promise.of(['baz']);

p2.concat(p3).map(function (x) {
  console.log(x); //=> [ 'bar', 'baz' ]
});

p.conjoin(p2).map(function (x) {
  console.log(x); //=> [ 'Foo', 'bar' ]
});

p.combine(p2).map(function (x) {
  console.log(x); //=> [ 'Foo', [ 'bar' ] ]
});

p.combine(p2).explode(function (x, y) {
  console.log(x); //=> Foo
  console.log(y); //=> [ 'bar' ]
});

Keywords

FAQs

Package last updated on 25 May 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc