{"name":"@open-draft/deferred-promise","version":"1.1.0","description":"A Promise that can be resolved/rejected elsewhere","main":"./build/index.js","typings":"./build/index.d.ts","scripts":{"test":"jest","prebuild":"rimraf ./build","build":"tsc","release":"release publish"},"keywords":["promise","defer","deferred","resolve","reject"],"author":{"name":"Artem Zakharchenko"},"license":"MIT","devDependencies":{"@ossjs/release":"^0.3.2","@types/jest":"^29.0.1","jest":"^29.0.3","rimraf":"^3.0.2","ts-jest":"^29.0.0","typescript":"^4.8.3"},"gitHead":"1fcebb20692f201ccf17e0d141905674de8d1c77","_id":"@open-draft/deferred-promise@1.1.0","_nodeVersion":"16.17.0","_npmVersion":"8.15.0","dist":{"integrity":"sha512-tPptq9KWSAAa08yLLh1v2rE39fn8Eq7qdZw44CEhs/b5FNBFyCX9idE9Iw0OCakBw2aIWykFRpp06vB9FgKsaw==","shasum":"69c447caf2f3592f99d5e5d058c47d6d60a52e8a","tarball":"http://registry-npm.ywwl.com/@open-draft/deferred-promise/download/@open-draft/deferred-promise-1.1.0.tgz","fileCount":8,"unpackedSize":10762,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQD/wYPq9TH4gw7rmvpOX3pf8zkBxijHS0Sl/xdIfDClNgIgHzTU8Z2uPUdHEbTB/7nc+0ZS5Yjn8KQc7DoW8DX+Ct4="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjMXvbACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmpe3Q/8DBDsWEinuhCgXqMvq+EcL1TdeR8H4GP2opa8sG3yd6DpwrVI\r\nFXkyVzcou+wuzlwlRaRsR3olXTaGl+LvZXi+NenWxohf/ZXaoUxSxrYq7+Ig\r\n3TiOlxH7Q7bW0AtgaQDqGsh2Snv3FDN1l5IOFu+KKrdQ2Yj4fTx+3xfwgGR5\r\n5IHwD5FGZHcFW7ey9K9ZLvPRZ2oH+J3/ZgNXTuFsp9LCJEAT5DMbZLGTdNRw\r\nr2wzWUnxz4F55FDJZp6jBuLnu4vMFQw79J0i5Yhgm5GB0f6K5nCzu0tDU4kx\r\nden/HMJH7BLx3SiWjI2uHJh5Hv5f04mt8A1yo3GA0uOc22Ku+1KVoqiiGvv8\r\nwH8/LyzXuioeBrLIprV42C551hWpaEHXHfIjO74EwphrLLgn4yqrU9OMlXSg\r\nPjkS91I0G6pxUUki0YEpdtlj68DXUHqO+DyRwwbORQxmbHb7r4/WQdlVe4pR\r\nPiyPRF9L0qT5ey/EM5xCXY5eQYQ1SViLBY4E2Ioo9BYcf8rRMLRkNRJGLheB\r\n4nTnekmc3eRaUhJepxJjurA5o9jOj8+d0dgAg1ddbw9cwMf9EmOo68WFoYIV\r\nbe2V3N/E6zy8XSSjGKcuFP8nrbmoQQfXj50QW/Bm0tHGq3Dclr2Uy/8WOU3T\r\n2yQFINRK/CtEfAaxhp9bRMvCp5Yks3YWdY4=\r\n=G/Ba\r\n-----END PGP SIGNATURE-----\r\n","size":3620,"noattachment":false,"key":"/@open-draft/deferred-promise/-/@open-draft/deferred-promise-1.1.0.tgz"},"_npmUser":{"name":"kettanaito","email":"kettanaito@gmail.com"},"directories":{},"maintainers":[{"name":"kettanaito","email":"kettanaito@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/deferred-promise_1.1.0_1664187355218_0.9423027718632235"},"_hasShrinkwrap":false,"readme":"# Deferred Promise\n\nThe `DeferredPromise` class is a Promise-compatible abstraction that defers resolving/rejecting promises to another closure. This class is primarily useful when one part of your system establishes as promise but another part of your system fulfills it.\n\n> This class is conceptually inspired by the [`createDeferredPromise()`](https://github.com/nodejs/node/blob/696fd4b14fc34cc2d01497a3abd9bb441b89be50/lib/internal/util.js#L468-L477) internal utility in Node.js. Unlike the Node.js implementation, however, `DeferredProimse` _extends_ a native `Promise`, allowing the consumer to handle deferred promises like regular promises (no `.promise` instance nesting).\n\n## Getting started\n\n```sh\nnpm install @open-draft/deferred-promise\n```\n\n## Documentation\n\n- [**`createDeferredExecutor()`**](#createdeferredexecutor)\n  - [`DeferredExecutor.state`](#deferredexecutorstate)\n  - [`DeferredExecutor.resolve()`](#deferredexecutorresolve)\n  - [`DeferredExecutor.reject()`](#deferredexecutorreject)\n  - [`DeferredExecutor.rejectionReason`](#deferredexecutorrejectionreason)\n- [**Class: `DeferredPromise`**](#class-deferredpromise)\n  - [`new DeferredPromise()`](#new-defferedpromise)\n  - [`deferredPromise.state`](#deferredpromisestate)\n  - [`deferredPromise.resolve()`](#deferredpromiseresolve)\n  - [`deferredPromise.reject()`](#deferredpromisereject)\n  - [`deferredPromise.rejectionReason`](#deferredpromiserejectionreason)\n\n---\n\n## `createDeferredExecutor()`\n\nCreates a Promise executor function that delegates its resolution to the current scope.\n\n```js\nimport { createDeferredExecutor } from '@open-draft/deferred-promise'\n\nconst executor = createDeferredExecutor()\nconst promise = new Promise(executor)\n\nexecutor.resolve('hello')\n// executor.reject(new Error('Reason'))\n```\n\nDeferred executor allows you to control any promise remotely and doesn't affect the Promise instance in any way. Similar to the [`DeferredPromise`](#class-deferredpromise) instance, the deferred executor exposes additional promise properties like `state`, `rejectionReason`, `resolve`, and `reject`. In fact, the `DeferredPromise` class is implemented on top of the deferred executor.\n\n```js\nconst executor = createDeferredExecutor()\nconst promise = new Promise(executor)\n\nexecutor.reject('reason')\n\nnextTick(() => {\n  console.log(executor.rejectionReason) // \"reason\"\n})\n```\n\n### `DeferredExecutor.state`\n\n- `<\"pending\" | \"fulfilled\" | \"rejected\">` **Default:** `\"pending\"`\n\n```js\nconst executor = createDeferredExecutor()\nconst promise = new Promise(executor)\n\nconsole.log(executor.state) // \"pending\"\n```\n\nCalling [`resolve()`](#deferredexecutorresolve) and [`reject()`](#deferredexecutorreject) methods of the executor transitions the state to \"fulfilled\" and \"rejected\" respectively.\n\n### `DeferredExecutor.resolve()`\n\nResolves the promise with a given value.\n\n```js\nconst executor = createDeferredExecutor()\nconst promise = new Promise(executor)\n\nconsole.log(executor.state) // \"pending\"\n\nexecutor.resolve()\n\n// The promise state is still \"pending\"\n// because promises are settled in the next microtask.\nconsole.log(executor.state) // \"pending\"\n\nnextTick(() => {\n  // In the next microtask, the promise's state is resolved.\n  console.log(executor.state) // \"fulfilled\"\n})\n```\n\n### `DeferredExecutor.reject()`\n\nRejects the promise with a given reason.\n\n```js\nconst executor = createDeferredExecutor()\nconst promise = new Promise(executor)\n\nexecutor.reject(new Error('Failed to fetch'))\n\nnextTick(() => {\n  console.log(executor.state) // \"rejected\"\n  console.log(executor.rejectionReason) // Error(\"Failed to fetch\")\n})\n```\n\nYou can access the rejection reason of the promise at any time by the [`rejectionReason`](#deferredexecutorrejectionreason) property of the deferred executor.\n\n### `DeferredExecutor.rejectionReason`\n\nReturns the reason of the promise rejection. If no reason has been provided to the `reject()` call, `undefined` is returned instead.\n\n```js\nconst executor = createDeferredExecutor()\nconst promise = new Promise(executor)\n\npromise.reject(new Error('Internal Server Error'))\n\nnextTick(() => {\n  console.log(promise.rejectionReason) // Error(\"Internal Server Error\")\n})\n```\n\n---\n\n## Class: `DeferredPromise`\n\n### `new DefferedPromise()`\n\nCreates a new instance of a deferred promise.\n\n```js\nimport { DeferredPromise } from '@open-draft/deferred-promise'\n\nconst promise = new DeferredPromise()\n```\n\nA deferred promise is a Promise-compatible class that constructs a regular Promise instance under the hood, controlling it via the [deferred executor](#createdeferredexecutor).\n\nA deferred promise is fully compatible with the regular Promise, both type- and runtime-wise, e.g. a deferred promise can be chained and awaited normally.\n\n```js\nconst promise = new DefferredPromise()\n  .then((value) => value.toUpperCase())\n  .then((value) => value.substring(0, 2))\n  .catch((error) => console.error(error))\n\nawait promise\n```\n\nUnlike the regular Promise, however, a deferred promise doesn't accept the `executor` function as the constructor argument. Instead, the resolution of the deferred promise is deferred to the current scope (thus the name).\n\n```js\nfunction getPort() {\n  // Notice that you don't provide any executor function\n  // when constructing a deferred promise.\n  const portPromise = new DeferredPromise()\n\n  port.on('open', (port) => {\n    // Resolve the deferred promise whenever necessary.\n    portPromise.resolve(port)\n  })\n\n  // Return the deferred promise immediately.\n  return portPromise\n}\n```\n\nUse the [`resolve()`](#deferredpromiseresolve) and [`reject()`](#deferredpromisereject) methods of the deferred promise instance to resolve and reject that promise respectively.\n\n### `deferredPromise.state`\n\nSee [`DeferredExecutor.state`](#deferredexecutorstate)\n\n### `deferredPromise.resolve()`\n\nSee [`DeferredExecutor.resolve()`](#deferredexecutorresolve)\n\n### `deferredPromise.reject()`\n\nSee [`DeferredExecutor.reject()`](#deferredexecutorreject)\n\n### `deferredPromise.rejectionReason`\n\nSee [`DeferredExecutor.rejectionReason`](#deferredexecutorrejectionreason)\n\n---\n\n## Mentions\n\n- [Jonas Kuske](https://github.com/jonaskuske) for the phenomenal work around improving Promise-compliance.\n","publish_time":1664187355371,"_cnpm_publish_time":1664187355371,"dist-tags":{"latest":"3.0.0"}}