{"_id":"@open-draft/deferred-promise","_rev":"3551439","name":"@open-draft/deferred-promise","description":"A Promise-compatible abstraction that defers resolving/rejecting promises to another closure.","dist-tags":{"latest":"2.2.0"},"maintainers":[{"name":"kettanaito","email":""}],"time":{"modified":"2024-09-19T02:43:11.000Z","created":"2022-09-12T22:54:10.400Z","2.2.0":"2023-09-07T14:40:53.461Z","2.1.0":"2022-12-07T15:59:11.638Z","2.0.0":"2022-11-24T12:32:44.683Z","1.3.0":"2022-10-20T13:00:26.516Z","1.2.1":"2022-10-20T10:39:21.420Z","1.2.0":"2022-10-20T10:28:38.251Z","1.1.1":"2022-09-26T10:49:11.941Z","1.1.0":"2022-09-26T10:15:55.371Z","1.0.0":"2022-09-12T22:54:10.400Z"},"users":{},"author":{"name":"Artem Zakharchenko"},"repository":{"type":"git","url":"git+https://github.com/open-draft/deferred-promise.git"},"versions":{"2.2.0":{"name":"@open-draft/deferred-promise","version":"2.2.0","description":"A Promise-compatible abstraction that defers resolving/rejecting promises to another closure.","main":"./build/index.js","types":"./build/index.d.ts","module":"./build/index.mjs","exports":{".":{"types":"./build/index.d.ts","require":"./build/index.js","default":"./build/index.mjs"}},"scripts":{"test":"jest","test:compliance":"export NODE_OPTIONS=--loader=tsx || set NODE_OPTIONS=--loader=tsx&& npx -y promises-aplus-tests ./test/aplus-tests-adapter.ts","prebuild":"rimraf ./build","build":"tsup","release":"release publish"},"keywords":["promise","defer","deferred","resolve","reject","executor"],"author":{"name":"Artem Zakharchenko"},"license":"MIT","repository":{"type":"git","url":"git+https://github.com/open-draft/deferred-promise.git"},"devDependencies":{"@ossjs/release":"^0.7.2","@types/jest":"^29.0.1","jest":"^29.0.3","rimraf":"^3.0.2","ts-jest":"^29.0.0","tsup":"^7.2.0","tsx":"^3.12.1","typescript":"^4.8.3"},"gitHead":"b30f1b683311030e92fcdd08d2bd6574d652b77f","bugs":{"url":"https://github.com/open-draft/deferred-promise/issues"},"homepage":"https://github.com/open-draft/deferred-promise#readme","_id":"@open-draft/deferred-promise@2.2.0","_nodeVersion":"18.17.1","_npmVersion":"9.6.7","dist":{"shasum":"4a822d10f6f0e316be4d67b4d4f8c9a124b073bd","size":5032,"noattachment":false,"key":"/@open-draft/deferred-promise/-/@open-draft/deferred-promise-2.2.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/@open-draft/deferred-promise/download/@open-draft/deferred-promise-2.2.0.tgz"},"_npmUser":{"name":"kettanaito","email":"kettanaito@gmail.com"},"directories":{},"maintainers":[{"name":"kettanaito","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/deferred-promise_2.2.0_1694097653217_0.847429663466426"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2023-09-07T14:40:53.461Z","publish_time":1694097653461,"_source_registry_name":"default","_cnpm_publish_time":1694097653461},"2.1.0":{"name":"@open-draft/deferred-promise","version":"2.1.0","description":"A Promise-compatible abstraction that defers resolving/rejecting promises to another closure.","main":"./build/index.js","typings":"./build/index.d.ts","scripts":{"test":"jest","test:compliance":"export NODE_OPTIONS=--loader=tsx || set NODE_OPTIONS=--loader=tsx&& npx -y promises-aplus-tests ./test/aplus-tests-adapter.ts","prebuild":"rimraf ./build","build":"tsc","release":"release publish"},"keywords":["promise","defer","deferred","resolve","reject","executor"],"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","tsx":"^3.12.1","typescript":"^4.8.3"},"gitHead":"fcd0feeed2221a84cb0354d9a57c42bc32e7ca4c","_id":"@open-draft/deferred-promise@2.1.0","_nodeVersion":"16.18.1","_npmVersion":"8.19.2","dist":{"shasum":"4fb33ebdf5c05a0e47a26490ed9037ca36275a66","size":6983,"noattachment":false,"key":"/@open-draft/deferred-promise/-/@open-draft/deferred-promise-2.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/@open-draft/deferred-promise/download/@open-draft/deferred-promise-2.1.0.tgz"},"_npmUser":{"name":"kettanaito","email":"kettanaito@gmail.com"},"directories":{},"maintainers":[{"name":"kettanaito","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/deferred-promise_2.1.0_1670428751474_0.6710658680983106"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2022-12-07T16:59:55.768Z","publish_time":1670428751638,"_cnpm_publish_time":1670428751638},"2.0.0":{"name":"@open-draft/deferred-promise","version":"2.0.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":"1fe382e81d5836769a4b5b2be78a28cffe0967f9","_id":"@open-draft/deferred-promise@2.0.0","_nodeVersion":"16.18.1","_npmVersion":"8.19.2","dist":{"shasum":"2d0194b78446782b7f8c93b1118d769d5032ef61","size":5455,"noattachment":false,"key":"/@open-draft/deferred-promise/-/@open-draft/deferred-promise-2.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/@open-draft/deferred-promise/download/@open-draft/deferred-promise-2.0.0.tgz"},"_npmUser":{"name":"kettanaito","email":"kettanaito@gmail.com"},"directories":{},"maintainers":[{"name":"kettanaito","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/deferred-promise_2.0.0_1669293164469_0.9373950003651759"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2022-11-24T13:07:25.883Z","publish_time":1669293164683,"_cnpm_publish_time":1669293164683},"1.3.0":{"name":"@open-draft/deferred-promise","version":"1.3.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":"3cd0983a6991bdc14e3411705c3099d7bdf352a6","_id":"@open-draft/deferred-promise@1.3.0","_nodeVersion":"16.17.1","_npmVersion":"8.15.0","dist":{"shasum":"c949fb4cb8a559e67392826de54d026d76d911fe","size":3683,"noattachment":false,"key":"/@open-draft/deferred-promise/-/@open-draft/deferred-promise-1.3.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/@open-draft/deferred-promise/download/@open-draft/deferred-promise-1.3.0.tgz"},"_npmUser":{"name":"kettanaito","email":"kettanaito@gmail.com"},"directories":{},"maintainers":[{"name":"kettanaito","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/deferred-promise_1.3.0_1666270826278_0.14752855941841414"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2022-10-20T14:15:01.682Z","publish_time":1666270826516,"_cnpm_publish_time":1666270826516},"1.2.1":{"name":"@open-draft/deferred-promise","version":"1.2.1","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":"1137d7181240f30069d6aee55f6a40a497e4e225","_id":"@open-draft/deferred-promise@1.2.1","_nodeVersion":"16.17.1","_npmVersion":"8.15.0","dist":{"shasum":"5d040557033c311982fed2e6053ee5610bcdf8a1","size":4081,"noattachment":false,"key":"/@open-draft/deferred-promise/-/@open-draft/deferred-promise-1.2.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/@open-draft/deferred-promise/download/@open-draft/deferred-promise-1.2.1.tgz"},"_npmUser":{"name":"kettanaito","email":"kettanaito@gmail.com"},"directories":{},"maintainers":[{"name":"kettanaito","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/deferred-promise_1.2.1_1666262361235_0.27401004813818863"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2022-10-20T12:05:09.535Z","publish_time":1666262361420,"_cnpm_publish_time":1666262361420},"1.2.0":{"name":"@open-draft/deferred-promise","version":"1.2.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":"9819dcd41629843dafd2a180ff41c572fe31381a","_id":"@open-draft/deferred-promise@1.2.0","_nodeVersion":"16.17.1","_npmVersion":"8.15.0","dist":{"shasum":"b45f688bf1dc41c19bae69aabeaa98b20402a19c","size":4058,"noattachment":false,"key":"/@open-draft/deferred-promise/-/@open-draft/deferred-promise-1.2.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/@open-draft/deferred-promise/download/@open-draft/deferred-promise-1.2.0.tgz"},"_npmUser":{"name":"kettanaito","email":"kettanaito@gmail.com"},"directories":{},"maintainers":[{"name":"kettanaito","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/deferred-promise_1.2.0_1666261718059_0.46956125204992416"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2022-10-20T12:05:08.990Z","publish_time":1666261718251,"_cnpm_publish_time":1666261718251},"1.1.1":{"name":"@open-draft/deferred-promise","version":"1.1.1","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":"05e7e938f540bcedd7c740cab0d58032e3657b07","_id":"@open-draft/deferred-promise@1.1.1","_nodeVersion":"16.17.0","_npmVersion":"8.15.0","dist":{"shasum":"6f5303076fa0ba9f8f929d6a0c0acfafb08a2075","size":3767,"noattachment":false,"key":"/@open-draft/deferred-promise/-/@open-draft/deferred-promise-1.1.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/@open-draft/deferred-promise/download/@open-draft/deferred-promise-1.1.1.tgz"},"_npmUser":{"name":"kettanaito","email":"kettanaito@gmail.com"},"directories":{},"maintainers":[{"name":"kettanaito","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/deferred-promise_1.1.1_1664189351792_0.2658581077826616"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2022-09-26T10:49:36.608Z","publish_time":1664189351941,"_cnpm_publish_time":1664189351941},"1.1.0":{"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":{"shasum":"69c447caf2f3592f99d5e5d058c47d6d60a52e8a","size":3620,"noattachment":false,"key":"/@open-draft/deferred-promise/-/@open-draft/deferred-promise-1.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/@open-draft/deferred-promise/download/@open-draft/deferred-promise-1.1.0.tgz"},"_npmUser":{"name":"kettanaito","email":"kettanaito@gmail.com"},"directories":{},"maintainers":[{"name":"kettanaito","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/deferred-promise_1.1.0_1664187355218_0.9423027718632235"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2022-09-26T10:22:30.282Z","publish_time":1664187355371,"_cnpm_publish_time":1664187355371},"1.0.0":{"name":"@open-draft/deferred-promise","version":"1.0.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":"512313d55168022b831a5cdba95dc03cf087c1eb","_id":"@open-draft/deferred-promise@1.0.0","_nodeVersion":"16.17.0","_npmVersion":"8.15.0","dist":{"shasum":"b6ede96830edc6fe78d55edaddd2213b4418c75b","size":3441,"noattachment":false,"key":"/@open-draft/deferred-promise/-/@open-draft/deferred-promise-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/@open-draft/deferred-promise/download/@open-draft/deferred-promise-1.0.0.tgz"},"_npmUser":{"name":"kettanaito","email":"kettanaito@gmail.com"},"directories":{},"maintainers":[{"name":"kettanaito","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/deferred-promise_1.0.0_1663023250240_0.6557680135906478"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2022-09-12T22:54:16.241Z","publish_time":1663023250400,"_cnpm_publish_time":1663023250400}},"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","_attachments":{},"homepage":"https://github.com/open-draft/deferred-promise#readme","bugs":{"url":"https://github.com/open-draft/deferred-promise/issues"},"license":"MIT"}