{"_id":"pacta","_rev":"33-ba683854b921b557f5d421a0144e5da0","name":"pacta","description":"An algebraic implementation of ECMAScript 2015 and Promises/A+ Promises.","dist-tags":{"latest":"0.9.0"},"versions":{"0.0.1":{"name":"pacta","description":"An algebraic implementation of Promises.","homepage":"https://github.com/mudge/pacta","author":{"name":"Paul Mucur","url":"http://mudge.name"},"keywords":["promises","monad","functor"],"version":"0.0.1","main":"./lib/pacta.js","devDependencies":{"mocha":"1.10.0"},"scripts":{"test":"mocha"},"repository":{"type":"git","url":"https://github.com/mudge/pacta.git"},"license":"MIT","engines":{"node":">=0.6"},"bugs":{"url":"https://github.com/mudge/pacta/issues"},"_id":"pacta@0.0.1","dist":{"shasum":"6de0192d1db6f78150d6405f340308bccd9804cc","tarball":"https://registry.npmjs.org/pacta/-/pacta-0.0.1.tgz","integrity":"sha512-u+XBlshJsONpUnNf+jnFwiZRWduAAc9dGUeoLOIXnmuRiGf8ggjH1AesE2BCIwi7Ht6IryCs1PVbtvqBCnF5mQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDDPUtBC+DsZXOjCRSz8NRovWmjjsYl6UzShUffz5qbMAIhAPu72DXjgyHi2XUmdvq4FC4z1p1TuXqC5+JZM0Cnpgel"}]},"_from":".","_npmVersion":"1.2.21","_npmUser":{"name":"mudge","email":"mudge@mudge.name"},"maintainers":[{"name":"mudge","email":"mudge@mudge.name"}],"directories":{}},"0.0.2":{"name":"pacta","description":"An algebraic implementation of Promises.","homepage":"https://github.com/mudge/pacta","author":{"name":"Paul Mucur","url":"http://mudge.name"},"keywords":["promises","monad","functor"],"version":"0.0.2","main":"./lib/pacta.js","dependencies":{},"devDependencies":{"mocha":"1.10.0"},"scripts":{"test":"mocha"},"repository":{"type":"git","url":"https://github.com/mudge/pacta.git"},"license":"MIT","engines":{"node":">=0.6"},"bugs":{"url":"https://github.com/mudge/pacta/issues"},"_id":"pacta@0.0.2","dist":{"shasum":"afd6a0c5d4c6de1c75b51d56b54c8cbca0732209","tarball":"https://registry.npmjs.org/pacta/-/pacta-0.0.2.tgz","integrity":"sha512-4/7Ooof9NDPhG48qBF+6+f7xJ4i6bNpryLXjGvY8qTWCL7d3ZdgSPudC+bKZ8v86ORn7dDDwwGkamQPK3mxkKQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCGSN/v/hbBYmevVnC0FO4+ObYO/+NskwCuboz2ohsGHwIgZ/yPCrvq4Gu9O9LSowvpmQDweS93UJmcrYp/EF7psRw="}]},"_from":".","_npmVersion":"1.2.21","_npmUser":{"name":"mudge","email":"mudge@mudge.name"},"maintainers":[{"name":"mudge","email":"mudge@mudge.name"}],"directories":{}},"0.0.3":{"name":"pacta","description":"An algebraic implementation of Promises.","homepage":"https://github.com/mudge/pacta","author":{"name":"Paul Mucur","url":"http://mudge.name"},"keywords":["promises","monad","functor"],"version":"0.0.3","main":"./lib/pacta.js","dependencies":{},"devDependencies":{"mocha":"1.10.0"},"scripts":{"test":"mocha"},"repository":{"type":"git","url":"https://github.com/mudge/pacta.git"},"license":"MIT","engines":{"node":">=0.6"},"bugs":{"url":"https://github.com/mudge/pacta/issues"},"_id":"pacta@0.0.3","dist":{"shasum":"01568ad6bb336c2084b33a256b1a80027e225487","tarball":"https://registry.npmjs.org/pacta/-/pacta-0.0.3.tgz","integrity":"sha512-E6OSkRihh8u9zJr98yY352816i6z6iztv4qb+Mb8j/LrtXp2Ni0lOOW0z3A8fGWnf4YPk3ale3QShgOPKr8ogw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBZEMIqFzha748g/gf3o4XlWjwm+6GXDJaY+2QIj9MC/AiEArxNE/JSxXpjjFzMAcmRjcn5oTRyUwVr+Qi5a3fJ81K8="}]},"_from":".","_npmVersion":"1.2.21","_npmUser":{"name":"mudge","email":"mudge@mudge.name"},"maintainers":[{"name":"mudge","email":"mudge@mudge.name"}],"directories":{}},"0.0.4":{"name":"pacta","description":"An algebraic implementation of Promises.","homepage":"https://github.com/mudge/pacta","author":{"name":"Paul Mucur","url":"http://mudge.name"},"keywords":["promises","monad","functor"],"version":"0.0.4","main":"./lib/pacta.js","dependencies":{},"devDependencies":{"mocha":"1.10.0"},"scripts":{"test":"mocha"},"repository":{"type":"git","url":"https://github.com/mudge/pacta.git"},"license":"MIT","engines":{"node":">=0.6"},"bugs":{"url":"https://github.com/mudge/pacta/issues"},"_id":"pacta@0.0.4","dist":{"shasum":"5b848a917cd4ca0017bd89e0179c4d8f4ced7d97","tarball":"https://registry.npmjs.org/pacta/-/pacta-0.0.4.tgz","integrity":"sha512-PotVNbo5UzgagwePovgOQ4tWngz3JADoPNj39bdX3pc2JYYIg6pYrjrsL073L8Pn9fhfkAz6jB71DCG51iXNwA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQD3dRbzrwKvCKJhg+5gEL5K94sskOw/IrP1bKGODhG1QAIhAPDSvUWm+pK6wwn5V2PiutE/uSakzfrQ1F1Y+CSrhM1v"}]},"_from":".","_npmVersion":"1.2.23","_npmUser":{"name":"mudge","email":"mudge@mudge.name"},"maintainers":[{"name":"mudge","email":"mudge@mudge.name"}],"directories":{}},"0.1.0":{"name":"pacta","description":"An algebraic, Promises/A+ compliant implementation of Promises.","homepage":"https://github.com/mudge/pacta","author":{"name":"Paul Mucur","url":"http://mudge.name"},"keywords":["promises","monad","functor","promises-aplus"],"version":"0.1.0","main":"./lib/pacta.js","dependencies":{},"devDependencies":{"mocha":"1.10.0","promises-aplus-tests":"1.3.1"},"scripts":{"test":"mocha"},"repository":{"type":"git","url":"https://github.com/mudge/pacta.git"},"license":"MIT","engines":{"node":">=0.6"},"bugs":{"url":"https://github.com/mudge/pacta/issues"},"_id":"pacta@0.1.0","dist":{"shasum":"2f4900a559a014655b3ee75691f5c79f82d866f2","tarball":"https://registry.npmjs.org/pacta/-/pacta-0.1.0.tgz","integrity":"sha512-v+uVBKJq+U/rNOygsLK13QpVFsmq6VCD/8dMpUk/NQYYDNcqnD/i+F2Kt4YD9PqyCrgqEFRt8X98QN+Hr1NZpQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDl5M3aaOD3j8IcbgjIUuydPOGR+O1wIXuMlKCGD9XP8gIhAPKkkvdvBioCrENxw4pYXVUHVFVS2GGCoZVs7EauHy4B"}]},"_from":".","_npmVersion":"1.2.24","_npmUser":{"name":"mudge","email":"mudge@mudge.name"},"maintainers":[{"name":"mudge","email":"mudge@mudge.name"}],"directories":{}},"0.2.0":{"name":"pacta","description":"An algebraic, Promises/A+ compliant implementation of Promises.","homepage":"https://github.com/mudge/pacta","author":{"name":"Paul Mucur","url":"http://mudge.name"},"keywords":["promises","monad","functor","promises-aplus"],"version":"0.2.0","main":"./lib/pacta.js","dependencies":{},"devDependencies":{"mocha":"1.10.0","promises-aplus-tests":"1.3.1"},"scripts":{"test":"mocha"},"repository":{"type":"git","url":"https://github.com/mudge/pacta.git"},"license":"MIT","engines":{"node":">=0.6"},"bugs":{"url":"https://github.com/mudge/pacta/issues"},"_id":"pacta@0.2.0","dist":{"shasum":"8e2f6d43641a3df12cc84128b2ded3b433f59ed6","tarball":"https://registry.npmjs.org/pacta/-/pacta-0.2.0.tgz","integrity":"sha512-ISCZKwl2Ua6f2+vvu3ubfQuRHSkANJl/KWulP65PXBA2xhfr78tjNYxD45WnmwZqs8M5Gqkw6fT7SFYh/SwWsA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC3SG1qGGBr1J7U3odwriiSwv4V7TFZw3pNwIGnss+GxgIgR2qdk808C6eUBgIcxmqD+FlhJ8ixJ5QL+ZGiFAe/PE8="}]},"_from":".","_npmVersion":"1.2.32","_npmUser":{"name":"mudge","email":"mudge@mudge.name"},"maintainers":[{"name":"mudge","email":"mudge@mudge.name"}],"directories":{}},"0.3.0":{"name":"pacta","description":"An algebraic, Promises/A+ compliant implementation of Promises.","homepage":"https://github.com/mudge/pacta","author":{"name":"Paul Mucur","url":"http://mudge.name"},"keywords":["promises","monad","functor","promises-aplus"],"version":"0.3.0","main":"./lib/pacta.js","dependencies":{},"devDependencies":{"mocha":"1.10.0","promises-aplus-tests":"1.3.1"},"scripts":{"test":"mocha"},"repository":{"type":"git","url":"https://github.com/mudge/pacta.git"},"license":"MIT","engines":{"node":">=0.6"},"bugs":{"url":"https://github.com/mudge/pacta/issues"},"_id":"pacta@0.3.0","dist":{"shasum":"6a0b11e96576704f96872738c4c71bcf86c73e02","tarball":"https://registry.npmjs.org/pacta/-/pacta-0.3.0.tgz","integrity":"sha512-oqTbEknEngtlIuIrecztqtNbDuSe5YOXfbH/E+cjXgVmn6A2iuEAYs6dWyoSXQSIPBSCkSyCKNyUvdgxO657wg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDpUOloEbbHJSWHLwDwnBoATvhqO6fV8uxCLlmNgz0HhAIgMA15dmAWsr8mRRJGrwD2HJJ5LnH8hfxBRq/kDtGxo/Y="}]},"_from":".","_npmVersion":"1.2.32","_npmUser":{"name":"mudge","email":"mudge@mudge.name"},"maintainers":[{"name":"mudge","email":"mudge@mudge.name"}],"directories":{}},"0.4.0":{"name":"pacta","description":"An algebraic, Promises/A+ compliant implementation of Promises.","homepage":"https://github.com/mudge/pacta","author":{"name":"Paul Mucur","url":"http://mudge.name"},"keywords":["promises","monad","functor","promises-aplus"],"version":"0.4.0","main":"./lib/pacta.js","dependencies":{},"devDependencies":{"mocha":"1.16.1","promises-aplus-tests":"2.0.3"},"scripts":{"test":"mocha"},"repository":{"type":"git","url":"https://github.com/mudge/pacta.git"},"license":"MIT","engines":{"node":">=0.6"},"bugs":{"url":"https://github.com/mudge/pacta/issues"},"_id":"pacta@0.4.0","dist":{"shasum":"368bf6e00589804906871424ae9a98e0b00ba2a0","tarball":"https://registry.npmjs.org/pacta/-/pacta-0.4.0.tgz","integrity":"sha512-44Dkc735zjs4lREDBqSBUru0ack5xn8uMmQRvbDlgcsMIEm/A5eJAU3M64R0o/IyzMaAp93eTzfxmcWHktZEeQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQD3lRLHPZipDzKzFQCqPiGx6Sy9k3kcYiQU0MjxpRJrBgIhANb0zVBZb+2ZENpqj/Hx9rMRaRXm2W4bORB1KGol53AA"}]},"_from":".","_npmVersion":"1.3.14","_npmUser":{"name":"mudge","email":"mudge@mudge.name"},"maintainers":[{"name":"mudge","email":"mudge@mudge.name"}],"directories":{}},"0.5.0":{"name":"pacta","description":"An algebraic, Promises/A+ compliant implementation of Promises.","homepage":"https://github.com/mudge/pacta","author":{"name":"Paul Mucur","url":"http://mudge.name"},"keywords":["promises","monad","functor","promises-aplus"],"version":"0.5.0","main":"./lib/pacta.js","dependencies":{},"devDependencies":{"mocha":"1.16.1","promises-aplus-tests":"2.0.3"},"scripts":{"test":"mocha"},"repository":{"type":"git","url":"https://github.com/mudge/pacta.git"},"license":"MIT","engines":{"node":">=0.6"},"bugs":{"url":"https://github.com/mudge/pacta/issues"},"_id":"pacta@0.5.0","dist":{"shasum":"37f245a8ce403c668404e78f74e76440ffb052a1","tarball":"https://registry.npmjs.org/pacta/-/pacta-0.5.0.tgz","integrity":"sha512-fpRM1xAhCciU37/bmDtTTPNhiPwVkGuWWmZea7SJAb/cfai0xkgw4JAXo+bgeI0T3Glk3AzbSxI8TCQCaHGqkw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIEPGbVFDPu2HxPnQAd/Kyy4F9nN+WILbj4YrWSshiFXmAiB4gY8qkxFQI8Pu8ohcPBYQJHC0F88g4LwuoZ4KukADkQ=="}]},"_from":".","_npmVersion":"1.3.21","_npmUser":{"name":"mudge","email":"mudge@mudge.name"},"maintainers":[{"name":"mudge","email":"mudge@mudge.name"}]},"0.5.1":{"name":"pacta","description":"An algebraic, Promises/A+ compliant implementation of Promises.","homepage":"https://github.com/mudge/pacta","author":{"name":"Paul Mucur","url":"http://mudge.name"},"keywords":["promises","monad","functor","promises-aplus"],"version":"0.5.1","main":"./lib/pacta.js","dependencies":{},"devDependencies":{"mocha":"1.16.1","promises-aplus-tests":"2.0.3"},"scripts":{"test":"mocha"},"repository":{"type":"git","url":"https://github.com/mudge/pacta.git"},"license":"MIT","engines":{"node":">=0.6"},"gitHead":"ab26720fb862078cdf4f7746c8e50d13056ed62a","bugs":{"url":"https://github.com/mudge/pacta/issues"},"_id":"pacta@0.5.1","_shasum":"e85d17530b82b9c44e4d79d68426bee38f946282","_from":".","_npmVersion":"2.5.1","_nodeVersion":"0.12.0","_npmUser":{"name":"mudge","email":"mudge@mudge.name"},"maintainers":[{"name":"mudge","email":"mudge@mudge.name"}],"dist":{"shasum":"e85d17530b82b9c44e4d79d68426bee38f946282","tarball":"https://registry.npmjs.org/pacta/-/pacta-0.5.1.tgz","integrity":"sha512-g/3A6bleJlLEEXm0DlD288yKjYwdyyTdA3o2pb/WfMY52Qto3+pXHvi/V2dsgLIkDZn9h9R2e5Ua+P44chNwWA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDIX/yzndWMkNoHaJrMt1Zt6JlTlXFHjn9iQ/AuB18/1AiBpuWQmr8mu9P5xPXrLBXeI0dACy4scaqGnRylAurIFsw=="}]}},"0.9.0":{"name":"pacta","description":"An algebraic implementation of ECMAScript 2015 and Promises/A+ Promises.","homepage":"https://github.com/mudge/pacta","author":{"name":"Paul Mucur","url":"http://mudge.name"},"keywords":["promises","monad","functor","promises-aplus"],"version":"0.9.0","main":"./lib/pacta.js","dependencies":{},"devDependencies":{"mocha":"1.16.1","promises-aplus-tests":"2.0.3"},"scripts":{"test":"mocha"},"repository":{"type":"git","url":"git+https://github.com/mudge/pacta.git"},"license":"MIT","engines":{"node":">=0.6"},"gitHead":"30ddd21bf5d4383b01f09e89bf49e295314bf6b3","bugs":{"url":"https://github.com/mudge/pacta/issues"},"_id":"pacta@0.9.0","_shasum":"cf435a852eda73e89df22b50a107733208f4d113","_from":".","_npmVersion":"2.14.4","_nodeVersion":"0.12.7","_npmUser":{"name":"mudge","email":"mudge@mudge.name"},"dist":{"shasum":"cf435a852eda73e89df22b50a107733208f4d113","tarball":"https://registry.npmjs.org/pacta/-/pacta-0.9.0.tgz","integrity":"sha512-EUxeYJ6qT2mDJMrYyNwFv17xJOf5fnzoRUqDCciG7MLyDiIaqPm1SDUbnMqs+gNhF4mo4S35+3uB+xXJujfwjw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIH3yMIqjEL/rR8lfgXfUvCOe+892fTAtlMilGA7OXiTsAiEAruHX46z/R2U5RibcG+0C545PcFmJxkGcK2jb/pgwBZU="}]},"maintainers":[{"name":"mudge","email":"mudge@mudge.name"}]}},"readme":"# Pacta [![Build Status](https://travis-ci.org/mudge/pacta.png?branch=master)](https://travis-ci.org/mudge/pacta)\n\nAn [algebraic][Fantasy Land] implementation of [ECMAScript 2015][ECMAScript]\nand [Promises/A+][A+] Promises in JavaScript for as many browsers and\n[Node.js](http://nodejs.org) versions as possible.\n\n**Current version:** 0.9.0  \n**Supported Node.js versions:** 0.6, 0.8, 0.10, 0.11, 0.12, 4.0, 4.1, 5.0  \n**Supported browsers:** Internet Explorer 6+, Firefox 3.6+, Chrome 14+, Opera 10.6+, Safari 4+, iOS 3+, Windows Phone 8.1, Android 2.2+\n\n```javascript\nvar promise = new Promise(function (resolve, reject) {\n    setTimeout(function () { resolve('Hello'); }, 5000);\n    setTimeout(function () { reject('Timeout!'); }, 60000);\n});\n\npromise\n    .then(function (value) { return value + ', World!'; })\n    .catch(function (reason) { return 'Sorry'; })\n    .then(console.log); //=> Hello, World!\n```\n\n## Installation\n\n```shell\n$ npm install pacta   # for Node.js\n$ bower install pacta # for the browser\n```\n\nAlternatively, include `pacta.js` via a `<script/>` in your page (Pacta also\nsupports using an [AMD](https://github.com/amdjs/amdjs-api/blob/master/AMD.md)\nAPI-compliant loader such as [RequireJS](http://requirejs.org/)).\n\n## Promises\n\nPromises can be thought of as objects representing a value that may not have\nbeen calculated yet (they are sometimes referred to as `Deferred`s).\n\nAn obvious example is the result of an asynchronous HTTP request: it's not\nclear *when* the request will be fulfilled but it will be at some point in the\nfuture. Having actual Promise objects representing these eventual values\nallows you to compose, transform and act on them without worrying about their\ntime or sequence of execution.\n\nAt their most basic, an empty promise can be created and resolved like so:\n\n```javascript\nvar Promise = require('pacta');\n\nvar p = new Promise(function (resolve) {\n    setTimeout(function () {\n        /* Populate the promise with its final value. */\n        resolve(1);\n    }, 1000);\n});\n```\n\nPromises can also be marked as `rejected` (viz. represent an error state) like\nso:\n\n```javascript\nvar p = new Promise(function (resolve, reject) {\n    /* Mark the promise as rejected with a reason. */\n    reject('The server could not be found.');\n});\n```\n\nConcretely, a promise can be represented by the following deterministic finite automaton:\n\n<p align=\"center\"><img src=\"images/dfa.png\" width=\"275\" height=\"192\" alt=\"\"></p>\n\nFor a worked example of using promises, see the [sample HTTP\nclient](https://github.com/mudge/pacta/blob/master/example/promised-http.js)\nand [two](https://github.com/mudge/pacta/blob/master/example/codenames.js)\n[example\nprograms](https://github.com/mudge/pacta/blob/master/example/codenames-2.js)\nincluded in Pacta.\n\n### ECMAScript 2015\n\nPacta's promises comply with the Promise API described in [ECMAScript\n2015][ECMAScript] and the [Promises/A+ specification][A+]:\n\n* [`new Promise(executor)`](#new-promiseexecutor) for constructing, resolving and rejecting promises;\n* [`Promise#then(onFulfilled, onRejected)`](#promisethenonfulfilled-onrejected) for binding callbacks on promise resolution or rejection (compliant with the [Promises/A+ specification][A+]);\n* [`Promise#catch(onRejected)`](#promisecatchonrejected) for dealing with rejected promises;\n* [`Promise.all(iterable)`](#promisealliterable) for returning a promise that is resolved when all of the promises in an iterable resolve, or rejects with the reason of the first rejected promise;\n* [`Promise.race(iterable)`](#promiseraceiterable) for returning a promise that resolves or rejects as soon as one of the promises in an iterable resolves or rejects;\n* [`Promise.reject(reason)`](#promiserejectreason) for constructing rejected promises;\n* [`Promise.resolve(value)`](#promiseresolvevalue) for constructing resolved promises.\n\n### Algebraic JavaScript\n\nThe aforementioned high level functions are implemented in terms of the\nalgebraic primitives defined in the [\"Fantasy Land\" Algebraic JavaScript\nSpecification][Fantasy Land]:\n\n* [Semigroups](https://github.com/puffnfresh/fantasy-land#semigroup) (through\n  [`Promise#concat`](#promiseconcatp) which concatenates promises containing semigroups such as\n  arrays and strings);\n* [Monoids](https://github.com/puffnfresh/fantasy-land#monoid) (through\n  [`Promise#empty`](#promiseempty) which returns an empty version of a promise that contains a\n  monoid);\n* [Functors](https://github.com/puffnfresh/fantasy-land#functor) (through\n  [`Promise#map`](#promisemapf));\n* [Applicative](https://github.com/puffnfresh/fantasy-land#applicative)\n  (through [`Promise#ap`](#promiseapp) and [`Promise.of`](#promiseofx));\n* [Chains](https://github.com/puffnfresh/fantasy-land#chain) (through [`Promise#chain`](#promisechainf));\n* [Monads](https://github.com/puffnfresh/fantasy-land#monad) (through all of\n  the above).\n\nThese different specifications can be thought of as different levels of\nabstraction with ECMAScript 2015 at the top and Fantasy Land at the bottom,\ne.g.\n\n| Specification   | Functions                                                                           |\n| --------------- | ----------------------------------------------------------------------------------- |\n| ECMAScript 2015 | `Promise.all`, `Promise.race`, `Promise.resolve`, `Promise.reject`, `Promise#catch` |\n| Promises/A+     | `Promise#then`                                                                      |\n| Fantasy Land    | `Promise#map`, `Promise#concat`, `Promise#chain`, etc.                              |\n\nPacta gives you access to all of these functions including the algebraic\nprimitives for composition into more expressive operations.\n\n### Working with lists of promises\n\nAs well as the standard [`Promise.all`](#promisealliterable) and\n[`Promise.race`](#promiseraceiterable), Pacta also provides the following\nfunctions for creating and working with Promises of lists:\n\n* [`Promise#conjoin`](#promiseconjoinp) to concatenate promises into a list of\n  values regardless of their original type meaning that non-Monoid types can\n  be combined with others (e.g. a promise of `'foo'` can be conjoined with\n  `[1, 2]` to produce `['foo', 1, 2]`);\n* [`Promise#append`](#promiseappendp) to append promises to an initial promise\n  of a list. This means that you can work more easily with multiple promises\n  of lists without joining them together (as would be done with `concat` and\n  `conjoin`), e.g.  appending a promise of `[2, 3]` to a promise of `[1]`\n  results in `[1, [2, 3]]` rather than `[1, 2, 3]`);\n* [`Promise#reduce`](#promisereducef-initialvalue) to\n  [reduce](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/Reduce)\n  a list within a promise;\n* [`Promise#spread`](#promisespreadf) to map over a promise's value but,\n  instead of receiving a single value, spread the promise's value across\n  separate arguments:\n\n```javascript\nPromise.all([1, 2]).spread(function (x, y) {\n    console.log(x); //=> 1\n    console.log(y); //=> 2\n});\n```\n\nIt also defines a monoid interface for `Array` and `String`, implementing\n`empty` such that:\n\n```javascript\nArray.empty();  //=> []\nString.empty(); //=> \"\"\n```\n\nSee [the test\nsuite](https://github.com/mudge/pacta/blob/master/test/pacta_test.js) for more\ninformation.\n\n[A+]: http://promises-aplus.github.io/promises-spec/\n[Fantasy Land]: https://github.com/puffnfresh/fantasy-land\n[Maybe]: https://en.wikipedia.org/wiki/Monad_(functional_programming)#The_Maybe_monad\n[ECMAScript]: http://www.ecma-international.org/ecma-262/6.0/#sec-promise-objects\n\n## API Documentation\n\n### `new Promise([executor])`\n\n```javascript\nvar promise = new Promise();\nvar promise = new Promise(function (resolve, reject) {\n    if (foo) {\n        resolve('Huzzah!');\n    } else {\n        reject('Oops!');\n    }\n});\n```\n\nCreate a new, unfulfilled promise that will eventually be populated with a\nvalue either by an optionally passed `executor` function (which is passed a\n`resolve` and a `reject` function) or by [`Promise#resolve`](#promiseresolvex)\nand [`Promise#reject`](#promiserejectreason-1).\n\n#### See also\n\n* [Promise - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)\n* [Promise ( executor ) - ECMAScript 2015 Language\n  Specification](http://www.ecma-international.org/ecma-262/6.0/#sec-promise-executor)\n\n### `Promise#then([onFulfilled[, onRejected]])`\n\n```javascript\npromise.then(function (value) {\n    return x * 2;\n}); //=> Promise.resolve(4)\n\npromise.then(function (value) {\n    return Promise.resolve(x * 2);\n}); //=> Promise.resolve(4)\n\npromise.then(function (value) {\n    console.log('Success!', value);\n}, function (reason) {\n    console.error('Error!', reason);\n});\n```\n\nAn implementation of the [Promises/A+ `then`\nmethod](http://promisesaplus.com/#the__method), taking an\noptional `onFulfilled` and `onRejected` function to call when the promise is\nfulfilled or rejected respectively.\n\nLike [`Promise#map`](#promisemapf), `then` returns a promise itself and can be\nchained.\n\n*Unlike* [`Promise#map`](#promisemapf), `then` will unwrap any promise that is\nreturned by an `onFulfilled` or `onRejected` function (making `then` behave\nlike [`Promise#chain`](#promisechainf)).\n\n#### See also\n\n* [Promise.prototype.then() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then)\n* [The `then` Method - Promises/A+](https://promisesaplus.com/#point-21)\n* [Promise.prototype.then ( onFulfilled , onRejected ) - ECMAScript 2015 Language Specification](http://www.ecma-international.org/ecma-262/6.0/#sec-promise.prototype.then)\n\n### `Promise#catch(onRejected)`\n\n```javascript\npromise.catch(function (reason) {\n    console.error('Error!', reason);\n});\n```\n\nAn implementation of ECMAScript 2015's `catch` method, equivalent to calling\n[`Promise#then`](#promisethenonfulfilled-onrejected) with an `undefined`\n`onFulfilled`.\n\n*Unlike* [`Promise#onRejected`](#promiseonrejectedf), `catch` will unwrap any\npromises returned from the `onRejected` handler (making `catch` behave like\n[`Promise#chainRejected`](#promisechainrejectedf)).\n\n#### See also\n\n* [Promise.prototype.catch() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch)\n* [Promise.prototype.catch ( onRejected ) - ECMAScript 2015 Language\n  Specification](http://www.ecma-international.org/ecma-262/6.0/#sec-promise.prototype.catch)\n\n### `Promise.resolve(value)`\n\n```javascript\nvar promise = Promise.resolve(5);\nvar promise = Promise.resolve(promise);\nvar promise = Promise.resolve(thenable);\n```\n\nAn implementation of ECMAScript 2015's `Promise.resolve` for returning a\npromise resolved with the given value.\n\n*Unlike* [`Promise.of`](#promiseofx), if the given `value` is itself a promise\nor thenable (viz. a value with a `then` method) then `Promise.resolve` will\nunwrap it, resolving with its eventual state.\n\nNote that this can be used to convert other promise implementations into Pacta\npromises (and is used internally by Pacta to do so).\n\n#### See also\n\n* [Promise.resolve() - JavaScript |\n  MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve)\n* [Promise.resolve ( x ) - ECMAScript 2015 Language\n  Specification](http://www.ecma-international.org/ecma-262/6.0/#sec-promise.resolve)\n\n### `Promise.reject(reason)`\n\n```javascript\nvar promise = Promise.reject('error!');\nvar promise = Promise.reject(new TypeError('Oops!'));\n```\n\nAn implementation of ECMAScript 2015's `Promise.reject` for returning a\npromise rejected with a given reason.\n\n#### See also\n\n* [Promise.reject() - JavaScript |\n  MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject)\n* [Promise.reject ( r ) - ECMAScript 2015 Language\n  Specification](http://www.ecma-international.org/ecma-262/6.0/#sec-promise.reject)\n\n### `Promise.all(iterable)`\n\n```javascript\nvar promise = new Promise(function (resolve, reject) {\n    setTimeout(function () { resolve('foo'), 1000);\n});\n\nPromise.all(['bar', Promise.resolve(7), promise]).then(function (values) {\n    console.log(values); //=> ['bar', 7, 'foo']\n});\n```\n\nAn implementation of ECMAScript 2015's `Promise.all` for returning a promise\nthat resolves when all of the promises in a given iterable have resolved or\nrejects with the reason of the first passed promise that rejects.\n\nNote that every element of the given iterable is passed to\n[`Promise.resolve`](#promiseresolvevalue) to coerce it to a Pacta promise\n(this includes other promises, thenables and raw values).\n\nIf any promise in the iterable is rejected, the resulting promise will\ninstantly reject with that promise's reason, e.g.\n\n```javascript\nvar promise = new Promise(function (resolve, reject) {\n    setTimeout(function () { reject('oops!'); }, 500);\n});\n\nPromise.all(['bar', promise]).catch(function (reason) {\n    console.error(reason); //=> 'oops!'\n});\n```\n\n#### See also\n\n* [Promise.all() - JavaScript |\n  MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all)\n* [Promise.all ( iterable ) - ECMAScript 2015 Language\n  Specification](http://www.ecma-international.org/ecma-262/6.0/#sec-promise.all)\n\n### `Promise.race(iterable)`\n\n```javascript\nvar p1 = new Promise(function (resolve) {\n        setTimeout(function () { resolve('second'); }, 1000);\n    }),\n    p2 = new Promise(function (resolve) {\n        setTimeout(function () { resolve('first!'); }, 500);\n    });\n\nPromise.race([p1, p2]).then(function (value) {\n    console.log(value); //=> 'first!'\n});\n```\n\nAn implementation of ECMAScript 2015's `Promise.race` to return a promise that\nresolves or rejects as soon as one of the promises in the iterable resolves or\nrejects with the value or reason from that promise.\n\n#### See also\n\n* [Promise.race() - JavaScript |\n  MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race)\n* [Promise.race ( iterable ) - ECMAScript 2015 Language\n  Specification](http://www.ecma-international.org/ecma-262/6.0/#sec-promise.race)\n\n### `Promise.of(x)`\n\n```javascript\nvar promise = Promise.of(1);\nvar promise = Promise.of('foo');\nvar promise = Promise.of(Promise.of(1));\n```\n\nCreate a new, fulfilled promise already populated with a value `x`.\n\n*Unlike* [`Promise.resolve`](#promiseresolvevalue), `Promise.of` will not unwrap `x`\nif it is a promise or thenable itself so it is possible to created nested\npromises.\n\n#### See also\n\n* [`of` method - Fantasy Land\n  Specification](https://github.com/fantasyland/fantasy-land#of-method)\n\n### `Promise#resolve(x)`\n\n```javascript\nvar promise = new Promise();\npromise.resolve(5);\n```\n\nPopulate a promise with the value `x` thereby resolving it.\n\n*This function can also be called as `Promise#fulfill`.*\n\n### `Promise#reject(reason)`\n\n```javascript\nvar promise = new Promise();\npromise.reject('Errored out!');\n```\n\nMark a promise as rejected, populating it with a reason.\n\n### `Promise#map(f)`\n\n```javascript\nvar promise = Promise.of(2);\n\npromise.map(function (x) {\n    console.log(x);\n\n    return x * 2;\n}); //=> Promise.of(4)\n\npromise.map(function (x) {\n    return Promise.of(x * 2);\n}); //=> Promise.of(Promise.of(4))\n```\n\nExecute a function `f` on the contents of the promise. This returns a new\npromise containing the result of applying `f` to the initial promise's value.\n\nIn [Haskell](http://www.haskell.org) notation, its type signature is:\n\n```haskell\nmap :: Promise a -> (a -> b) -> Promise b\n```\n\nNote that this is the primary way of acting on the value of a promise: you can\nuse side-effects within your given function (e.g. `console.log`) as well as\nmodifying the value and returning it in order to affect the returning\npromise.\n\nNote that any uncaught exceptions during the execution of `f` will result in\nthe promise being `rejected` with the exception as its `reason`.\n\n#### See also\n\n* [`map` method - Fantasy Land\n  Specification](https://github.com/fantasyland/fantasy-land#map-method)\n\n### `Promise#onRejected(f)`\n\n```javascript\nvar p = new Promise();\np.reject('Error!');\np.onRejected(function (reason) {\n    console.error('Failed:', reason);\n});\n```\n\nIdentical to [`Promise#map`](#promisemapf) but only executed when a promise is\nrejected rather than resolved.\n\nNote that `onRejected` returns a promise itself that is fulfilled by the given\nfunction, `f`. In this way, you can gracefully recover from errors like so:\n\n```javascript\nvar p = new Promise();\np.reject('Error!');\n\np.onRejected(function (reason) {\n    return 'Some safe default';\n}).map(console.log);\n//=> Logs \"Some safe default\"\n```\n\nLike [`Promise#map`](#promisemapf), any uncaught exceptions within `f` will\nresult in a `rejected` promise:\n\n```javascript\nvar p = new Promise();\np.reject('Error!');\n\np.onRejected(function (reason) {\n    throw 'Another error!';\n}).onRejected(console.log);\n//=> Logs \"Another error!\"\n```\n\n### `Promise#chain(f)`\n\n```javascript\nvar promise = Promise.of(2);\n\npromise.chain(function (x) { return Promise.of(x * 2); }); //=> Promise.of(4)\n```\n\nExecute a function `f` with the value of the promise. This differs from\n[`Promise#map`](#promisemapf) in that the function *must* return a promise\nitself.\n\nIts type signature is:\n\n```haskell\nchain :: Promise a -> (a -> Promise b) -> Promise b\n```\n\n#### See also\n\n* [`chain` method - Fantasy Land\n  Specification](https://github.com/fantasyland/fantasy-land#chain-method)\n\n### `Promise#chainRejected(f)`\n\n```javascript\nvar promise = new Promise(function (resolve, reject) {\n    reject('error!');\n});\n\npromise.chainRejected(function (reason) {\n    return Promise.of('Phew!');\n}); //=> Promise.of('Phew!')\n```\n\nIdentical to [`Promise#chain`](#promisechainf) but only executed when a\npromise is rejected rather than resolved.\n\n*This function can also be called as `Promise#chainError`.*\n\n### `Promise#ap(p)`\n\n```javascript\nvar promise = Promise.of(function (x) { return x * 2; }),\n    promise2 = Promise.of(2);\n\npromise.ap(promise2); //=> Promise.of(4)\n```\n\nOn a promise containing a function, call that function with a promise `p`\ncontaining a value.\n\nIts type signature is:\n\n```haskell\nap :: Promise (a -> b) -> Promise a -> Promise b\n```\n\n#### See also\n\n* [`ap` method - Fantasy Land Specification](https://github.com/fantasyland/fantasy-land#ap-method)\n\n### `Promise#empty()`\n\n```javascript\nvar promise = Promise.of('woo');\n\npromise.empty(); //=> Promise.of('')\n```\n\nOn a promise containing a monoid (viz. something with an `empty()` function on\nitself or its constructor like `Array` or `String`), return a new promise with\nan empty version of the initial value.\n\n(Pacta ships with Monoid implementations for `Array` and `String` by default.)\n\n#### See also\n\n* [`empty` method - Fantasy Land Specification](https://github.com/fantasyland/fantasy-land#empty-method)\n\n### `Promise#concat(p)`\n\n```javascript\nvar promise = Promise.of('foo'),\n    promise2 = Promise.of('bar');\n\npromise.concat(promise2); //=> Promise.of('foobar')\n```\n\nConcatenate the promise with another promise `p` into one containing both\nvalues concatenated together. This will work for any promise containing a\nsemigroup (viz. a value that supports `concat`) such as `String` or `Array`.\nNote that [`concat`'s usual\nbehaviour](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/concat)\nof joining arrays, etc. applies.\n\nIts type signature is:\n\n```haskell\nconcat :: Promise a -> Promise a -> Promise a\n```\n\nIf either of the original two promises is rejected, the resulting concatenated\npromise will also be rejected. Note that only the first rejection will count\nas further rejections will be ignored.\n\n### See also\n\n* [`Promise#conjoin`](#promiseconjoinp)\n* [`Promise#append`](#promiseappendp)\n* [`concat` method - Fantasy Land\n  Specification](https://github.com/fantasyland/fantasy-land#concat-method)\n\n### `Promise#conjoin(p)`\n\n```javascript\nvar promise = Promise.of(1),\n    promise2 = Promise.of([2, 3]);\n\npromise.conjoin(promise2); //=> Promise.of([1, 2, 3])\n```\n\nConjoin the promise with another promise `p`, converting their values to\narrays if needed (e.g. `'foo'` into `['foo']`). This differs from\n[`Promise#concat`](#promiseconcatp) which only works on promises of values\nthat are semigroups themselves.\n\nAll values are coerced to arrays using `[].concat`.\n\n### `Promise#append(p)`\n\n```javascript\nvar promise = Promise.of([]),\n    promise2 = Promise.of([1]);\n\npromise.append(promise2); //=> Promise.of([[1]])\n```\n\nOn a promise of a list, append another promise `p`'s value to it without\njoining (e.g. appending `[1]` to `[]` results in `[[1]]`).\n\nThis is particularly useful when dealing with several promises containing\nlists and you want to keep them separated instead of being merged into one as\nwould happen with [`Promise#concat`](#promiseconcatp) and\n[`Promise#conjoin`](#promiseconjoinp).\n\n### `Promise#reduce(f[, initialValue])`\n\n```javascript\nvar promise = Promise.of([1, 2, 3]);\n\npromise.reduce(function (acc, e) {\n    return acc + e;\n}, 0); //=> Promise.of(6)\n```\n\nOn a promise containing an array,\n[reduce](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/Reduce)\nits value, returning a promise of the resulting value. This defers to the\nunderlying signature of `reduce` taking a function `f` and an optional\n`initialValue`.\n\n### `Promise#spread(f)`\n\n```javascript\nvar promise = Promise.of([1, 2]);\n\npromise.spread(function (x, y) {\n    return x + y;\n}); //=> Promise.of(3)\n```\n\nSimilar to [`Promise#map`](#promisemapf), apply a function `f` to a promise of\na list but, instead of receiving a single argument, pass each value of the\nlist to the function separately.\n\n## Contributors\n\n* Fixes to `chain`, `chainError` and `empty` were contributed by [Ben\n  Schulz](https://github.com/benschulz);\n* `mapError` and `chainError` were contributed by [Rodolphe\n  Belouin](https://github.com/rbelouin).\n\n## Acknowledgements\n\n[James\nCoglan](http://blog.jcoglan.com/2013/03/30/callbacks-are-imperative-promises-are-functional-nodes-biggest-missed-opportunity/)\nand [Aanand Prasad](http://aanandprasad.com/articles/negronis/) convinced me\nto explore the idea of monadic promises and [Brian McKenna's \"Fantasy Land\"\nspecification](https://github.com/puffnfresh/fantasy-land) and\n[feedback](https://github.com/mudge/pacta/issues/1) were essential.\n\n[![Fantasy Land](images/fantasy-land.png)][Fantasy Land] [![Promises/A+](images/promises-aplus.png)][A+]\n\n## License\n\nCopyright © 2013—2015 Paul Mucur.\n\nDistributed under the MIT License.\n\n","maintainers":[{"name":"mudge","email":"mudge@mudge.name"}],"time":{"modified":"2022-06-23T07:18:21.340Z","created":"2013-05-23T16:09:03.921Z","0.0.1":"2013-05-23T16:09:06.467Z","0.0.2":"2013-05-25T11:24:45.913Z","0.0.3":"2013-05-25T19:48:11.174Z","0.0.4":"2013-05-25T20:47:49.661Z","0.1.0":"2013-06-01T13:34:10.612Z","0.2.0":"2013-06-20T18:04:52.686Z","0.3.0":"2013-08-04T14:29:43.165Z","0.4.0":"2013-12-24T11:55:12.985Z","0.5.0":"2014-01-23T12:24:49.987Z","0.5.1":"2015-03-11T23:14:48.711Z","0.9.0":"2015-11-07T14:57:24.831Z"},"author":{"name":"Paul Mucur","url":"http://mudge.name"},"repository":{"type":"git","url":"git+https://github.com/mudge/pacta.git"},"readmeFilename":"README.md","homepage":"https://github.com/mudge/pacta","keywords":["promises","monad","functor","promises-aplus"],"bugs":{"url":"https://github.com/mudge/pacta/issues"},"license":"MIT"}