{"name":"@tootallnate/once","version":"1.1.1","description":"Creates a Promise that waits for a single event","main":"./dist/index.js","types":"./dist/index.d.ts","scripts":{"prebuild":"rimraf dist","build":"tsc","test":"mocha --reporter spec","test-lint":"eslint src --ext .js,.ts","prepublishOnly":"npm run build"},"repository":{"type":"git","url":"git://github.com/TooTallNate/once.git"},"keywords":[],"author":{"name":"Nathan Rajlich","email":"nathan@tootallnate.net","url":"http://n8.io/"},"license":"MIT","bugs":{"url":"https://github.com/TooTallNate/once/issues"},"devDependencies":{"@types/node":"^12.12.11","@typescript-eslint/eslint-plugin":"1.6.0","@typescript-eslint/parser":"1.1.0","eslint":"5.16.0","eslint-config-airbnb":"17.1.0","eslint-config-prettier":"4.1.0","eslint-import-resolver-typescript":"1.1.1","eslint-plugin-import":"2.16.0","eslint-plugin-jsx-a11y":"6.2.1","eslint-plugin-react":"7.12.4","mocha":"^6.2.2","rimraf":"^3.0.0","typescript":"^3.7.3"},"engines":{"node":">= 6"},"gitHead":"7cf120c15c61306b143eee6ba388e18c2af1f434","homepage":"https://github.com/TooTallNate/once#readme","_id":"@tootallnate/once@1.1.1","_nodeVersion":"12.16.1","_npmVersion":"6.13.4","dist":{"integrity":"sha512-8foiU77JL9wR2yPZwecqN2YdJp/olC4CBVEaHdWWlz5rMQZcVEA3aXxlbfbUACVVxVwtTle3eYSnpBeKvIYcIg==","shasum":"e94fe74465c49c36094f403b56df7ae99861f466","tarball":"http://registry-npm.ywwl.com/@tootallnate/once/download/@tootallnate/once-1.1.1.tgz","fileCount":4,"unpackedSize":3769,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJenjC0CRA9TVsSAnZWagAAtOcQAIxg+NzYDHDYZL4UOt8x\nvTcAu468CYxP9AmpqETGt0YyZweCdqKKVDV5hCV8kKUrmESikDf0oaz41aLL\nfskB6IXqzAOuk2RyEtoHUcJBtmXYoAfPfZj8MolDQOMA7ssjzp/vD6YZfOmr\neT3ZGXiffo3EXfnCcfr9RmHCc4uY2lCdYkp9iMeoIXMwxKwLw3IMtQmB5vpZ\nw5dBvrC/G7GWyZ0tE5KDHNRzhbRLq+iq4b5zvo4nzCz06HUTB+jPQP1TxUaI\nIRY4WAlOmPWbS5ZB+fW9aVEB+DgVgJrGbzQDoky4vx0Z+pURLWQR3aP+TTRS\nqK4Q6+C5GyK9/7skL6mUymgWvFfkxx0Piwz9WdT4L345BZDDrh2o6Da99A0l\nvkJxTe1sXY8IXnfd4P6tfnCSx1ubPzY7UO0Nr9RW5oSmfs/Fqp4JWK0/VLIF\nHiWXMBOZA/IFu6XtLbnmQhOlxWF2dKplUQeq15Pxy46Kvc94to941m6x4XwO\n+Dk4dAXQIKrTcYnNL/53u1A/0ZKM8YrC0Ij0Zvtcanp31BOjYBohTLvmRuOe\n+fizOzlMILGz/VyNNDHZ97nQk6OSUEzSfUycXMQmupAoCnQ4OAxqJSxRHH73\n4Wt8Au/9WrvasZEakbb1qQVeQvRELzxMj8iArzDE412YVCr4lCeTVSzm9ghO\n88fb\r\n=odp2\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC3JQrEE0GP04p8SUgsuEK47i6x40iKJcqmFS5IXZsDWgIgf9ZqiwIVYwpkFCauUaJR2KCuEb4oftB69KADGsP+qgI="}],"size":1579,"noattachment":false,"key":"/@tootallnate/once/-/@tootallnate/once-1.1.1.tgz"},"maintainers":[{"name":"tootallnate","email":"nathan@tootallnate.net"}],"_npmUser":{"name":"tootallnate","email":"nathan@tootallnate.net"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/once_1.1.1_1587425459623_0.22249323277893485"},"_hasShrinkwrap":false,"readme":"# @tootallnate/once\n\n### Creates a Promise that waits for a single event\n\n## Installation\n\nInstall with `npm`:\n\n```bash\n$ npm install @tootallnate/once\n```\n\n## API\n\n### once(emitter: EventEmitter, name: string, opts?: OnceOptions): Promise&lt;[...Args]&gt;\n\nCreates a Promise that waits for event `name` to occur on `emitter`, and resolves\nthe promise with an array of the values provided to the event handler. If an\n`error` event occurs before the event specified by `name`, then the Promise is\nrejected with the error argument.\n\n```typescript\nimport once from '@tootallnate/once';\nimport { EventEmitter } from 'events';\n\nconst emitter = new EventEmitter();\n\nsetTimeout(() => {\n    emitter.emit('foo', 'bar');\n}, 100);\n\nconst [result] = await once(emitter, 'foo');\nconsole.log({ result });\n// { result: 'bar' }\n```\n\n#### Promise Strong Typing\n\nThe main feature that this module provides over other \"once\" implementations is that\nthe Promise that is returned is _**strongly typed**_ based on the type of `emitter`\nand the `name` of the event. Some examples are shown below.\n\n_The process \"exit\" event contains a single number for exit code:_\n\n```typescript\nconst [code] = await once(process, 'exit');\n//     ^ number\n```\n_A child process \"exit\" event contains either an exit code or a signal:_\n\n```typescript\nconst child = spawn('echo', []);\nconst [code, signal] = await once(child, 'exit');\n//     ^ number | null\n//           ^ string | null\n```\n\n_A forked child process \"message\" event is type `any`, so you can cast the Promise directly:_\n\n```typescript\nconst child = fork('file.js');\n\n// With `await`\nconst [message, _]: [WorkerPayload, unknown] = await once(child, 'message');\n\n// With Promise\nconst messagePromise: Promise<[WorkerPayload, unknown]> = once(child, 'message');\n\n// Better yet would be to leave it as `any`, and validate the payload\n// at runtime with i.e. `ajv` + `json-schema-to-typescript`\n```\n\n_If the TypeScript definition does not contain an overload for the specified event name, then the Promise will have type `unknown[]` and your code will need to narrow the result manually:_\n\n```typescript\ninterface CustomEmitter extends EventEmitter {\n    on(name: 'foo', listener: (a: string, b: number) => void): this;\n}\n\nconst emitter: CustomEmitter = new EventEmitter();\n\n// \"foo\" event is a defined overload, so it's properly typed\nconst fooPromise = once(emitter, 'foo');\n//    ^ Promise<[a: string, b: number]>\n\n// \"bar\" event in not a defined overload, so it gets `unknown[]`\nconst barPromise = once(emitter, 'bar');\n//    ^ Promise<unknown[]>\n```\n\n### OnceOptions\n\n-   `signal` - `AbortSignal` instance to unbind event handlers before the Promise has been fulfilled.\n","publish_time":1587425459787,"_cnpm_publish_time":1587425459787,"dist-tags":{"latest":"3.0.1","legacy":"2.0.1"}}