{"id":13623367,"url":"https://github.com/jameslnewell/wait-for-event","last_synced_at":"2025-03-21T01:32:08.631Z","repository":{"id":42722109,"uuid":"21107274","full_name":"jameslnewell/wait-for-event","owner":"jameslnewell","description":"Wait for events to be triggered.","archived":false,"fork":false,"pushed_at":"2023-01-06T13:27:21.000Z","size":1431,"stargazers_count":6,"open_issues_count":12,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-14T08:03:56.782Z","etag":null,"topics":["emitter","eventemitter","events","until","wait"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jameslnewell.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-06-22T22:23:39.000Z","updated_at":"2023-01-31T16:34:28.000Z","dependencies_parsed_at":"2023-02-06T04:16:22.522Z","dependency_job_id":null,"html_url":"https://github.com/jameslnewell/wait-for-event","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameslnewell%2Fwait-for-event","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameslnewell%2Fwait-for-event/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameslnewell%2Fwait-for-event/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameslnewell%2Fwait-for-event/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jameslnewell","download_url":"https://codeload.github.com/jameslnewell/wait-for-event/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244097235,"owners_count":20397567,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["emitter","eventemitter","events","until","wait"],"created_at":"2024-08-01T21:01:30.954Z","updated_at":"2025-03-21T01:32:08.366Z","avatar_url":"https://github.com/jameslnewell.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# wait-for-event\n\n![main](https://github.com/jameslnewell/wait-for-event/workflows/main/badge.svg)\n\nWait for events to be emitted.\n\n- works with [`EventEmitter`](https://nodejs.org/api/events.html#events_class_eventemitter)s and [`Stream`](https://nodejs.org/api/stream.html)s in NodeJS\n- works with [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget)s in the browser (via a bundler)\n\n## Installation\n\nNPM:\n\n```bash\nnpm install --save wait-for-event\n```\n\nYarn:\n\n```\nyarn add wait-for-event\n```\n\n## API\n\n### `.waitFor(event, emitter, [callback])`\n\nWait for the event to be emitted by the emitter.\n\n- `event` - The event name\n- `emitter` - The event emitter\n- `callback` - An optional callback which is called when the function resolves or rejects\n\nResolves when the `event` is emitted by the `emitter`. Rejects when the `error` event is emitted by the `emitter`.\n\n#### Example\n\n```js\nimport {EventEmitter} from 'events';\nimport {waitFor} from 'wait-for-event';\n\nconst emitter = new EventEmitter();\n\nsetImmediate(() =\u003e emitter.emit('done'));\n\nawait waitFor('done', emitter);\n```\n\n### `.waitForAll(event, emitters, [callback])`\n\nWait for the event to be emitted by _each_ of the emitters.\n\n- `event` - The event name\n- `emitters` - The event emitters\n- `callback` - An optional callback which is called when the function resolves or rejects\n\nResolves when `emitters` is an empty array. Resolves when the `event` is emitted by each of the `emitters`. Rejects when the `error` event is emitted by any of the `emitters`.\n\n#### Example\n\n```js\nimport {EventEmitter} from 'events';\nimport {waitForAll} from 'wait-for-event';\n\nconst emitters = [new EventEmitter(), new EventEmitter(), new EventEmitter()];\n\nsetImmediate(() =\u003e {\n  emitters[0].emit('done');\n  emitters[1].emit('done');\n  emitters[2].emit('done');\n});\n\nawait waitForAll('done', emitters);\n```\n\n### .waitForFirst(event, emitters, callback)\n\nWait for the event to be emitted by _any_ of the emitters.\n\n- `event` - The event name\n- `emitters` - The event emitters\n- `callback` - An optional callback which is called when the function resolves or rejects\n\nResolves when `emitters` is an empty array. Resolves when the `event` is emitted by _any_ of the `emitters`. Rejects when the `error` event is emitted by _any_ of the `emitters`.\n\n```js\nimport {EventEmitter} from 'events';\nimport {waitForFirst} from 'wait-for-event';\n\nconst emitters = [new EventEmitter(), new EventEmitter(), new EventEmitter()];\n\nsetImmediate(() =\u003e {\n  emitters[1].emit('done');\n});\n\nawait waitForFirst('done', emitters);\n```\n\n### `.waitForLull(event, emitters, [period,] [callback])`\n\nWait for the event to stop being emitted by _any_ of the emitters for a period of time.\n\n- `event` - The event name\n- `emitters` - The event emitters\n- `period` - The period of time the function will wait for no events to be emitted\n- `callback` - An optional callback which is called when the function resolves or rejects\n\nResolves when the period has elapsed and no events have been emitted. Rejects when the `error` event is emitted by _any_ of the `emitters`.\n\n```js\nimport {EventEmitter} from 'events';\nimport {waitForLull} from 'wait-for-event';\n\nconst emitters = [new EventEmitter(), new EventEmitter(), new EventEmitter()];\n\nsetImmediate(() =\u003e {\n  emitters[0].emit('msg');\n  emitters[1].emit('msg');\n  emitters[2].emit('msg');\n  emitters[2].emit('msg');\n  emitters[3].emit('msg');\n});\n\nawait waitForLull('msg', emitters, 3000);\n```\n\n## Change log\n\n# 2.0.0\n\n- **changed:** The handling of error events has changed. If any emmitter emits an error event then the method rejects immediately with the error\n- **changed:** Rewritten in Typescript\n- **added:** Added support for `Promise`s\n\n# 1.0.0\n\n- **changed:** Removed the `each` callback from `waitForAll()` - you can register your own event handlers on the emitters to achieve the same outcome.\n- **changed:** `waitForAll()` now handles `error` events\n- **changed:** Removed support for ComponentJS\n- **added:** `waitForFirst()`\n- **added:** `waitForLull()`\n- **fix:** `waitForAll()` no longer uses the primative method of counting the number of events that have been emitted - this method results in the callback being incorrectly called if an emitter emits the same event more than once.\n\n## License\n\nCopyright 2014 James Newell\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjameslnewell%2Fwait-for-event","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjameslnewell%2Fwait-for-event","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjameslnewell%2Fwait-for-event/lists"}