{"id":20842804,"url":"https://github.com/mudge/pacta","last_synced_at":"2026-04-01T23:34:57.841Z","repository":{"id":57317887,"uuid":"10238254","full_name":"mudge/pacta","owner":"mudge","description":"An algebraic implementation of ECMAScript 2015 and Promises/A+ Promises in JavaScript for as many browsers and Node.js versions as possible","archived":false,"fork":false,"pushed_at":"2015-11-07T14:56:21.000Z","size":513,"stargazers_count":71,"open_issues_count":0,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2026-03-05T12:33:53.896Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://npmjs.org/package/pacta","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mudge.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-05-23T07:27:13.000Z","updated_at":"2022-11-19T19:55:05.000Z","dependencies_parsed_at":"2022-08-25T20:40:26.663Z","dependency_job_id":null,"html_url":"https://github.com/mudge/pacta","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/mudge/pacta","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudge%2Fpacta","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudge%2Fpacta/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudge%2Fpacta/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudge%2Fpacta/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mudge","download_url":"https://codeload.github.com/mudge/pacta/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mudge%2Fpacta/sbom","scorecard":{"id":667166,"data":{"date":"2025-08-11","repo":{"name":"github.com/mudge/pacta","commit":"30ddd21bf5d4383b01f09e89bf49e295314bf6b3"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3,"checks":[{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Code-Review","score":0,"reason":"Found 1/13 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: BSD 3-Clause \"New\" or \"Revised\" License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 21 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}}]},"last_synced_at":"2025-08-21T18:28:33.342Z","repository_id":57317887,"created_at":"2025-08-21T18:28:33.342Z","updated_at":"2025-08-21T18:28:33.342Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31292974,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-01T21:15:39.731Z","status":"ssl_error","status_checked_at":"2026-04-01T21:15:34.046Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-18T01:25:33.336Z","updated_at":"2026-04-01T23:34:57.824Z","avatar_url":"https://github.com/mudge.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"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); //=\u003e 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 `\u003cscript/\u003e` 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\u003cp align=\"center\"\u003e\u003cimg src=\"images/dfa.png\" width=\"275\" height=\"192\" alt=\"\"\u003e\u003c/p\u003e\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); //=\u003e 1\n    console.log(y); //=\u003e 2\n});\n```\n\nIt also defines a monoid interface for `Array` and `String`, implementing\n`empty` such that:\n\n```javascript\nArray.empty();  //=\u003e []\nString.empty(); //=\u003e \"\"\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}); //=\u003e Promise.resolve(4)\n\npromise.then(function (value) {\n    return Promise.resolve(x * 2);\n}); //=\u003e 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); //=\u003e ['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); //=\u003e '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); //=\u003e '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}); //=\u003e Promise.of(4)\n\npromise.map(function (x) {\n    return Promise.of(x * 2);\n}); //=\u003e 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 -\u003e (a -\u003e b) -\u003e 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//=\u003e 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//=\u003e 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); }); //=\u003e 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 -\u003e (a -\u003e Promise b) -\u003e 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}); //=\u003e 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); //=\u003e 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 -\u003e b) -\u003e Promise a -\u003e 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(); //=\u003e 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); //=\u003e 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 -\u003e Promise a -\u003e 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); //=\u003e 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); //=\u003e 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); //=\u003e 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}); //=\u003e 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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmudge%2Fpacta","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmudge%2Fpacta","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmudge%2Fpacta/lists"}