Skip to content

Comments

Add support for filter predicate in once() (#122)#124

Merged
sindresorhus merged 3 commits intosindresorhus:mainfrom
faizanu94:feat/once-filter-predicate
Jun 20, 2025
Merged

Add support for filter predicate in once() (#122)#124
sindresorhus merged 3 commits intosindresorhus:mainfrom
faizanu94:feat/once-filter-predicate

Conversation

@faizanu94
Copy link
Contributor

Add support for filter predicate in once()

This change extends once(eventNames, predicate) so you can pass an optional filter function. When provided, once will:

• ignore any events for which predicate(data) returns false
• unsubscribe and resolve on the first event where predicate(data) returns true

If no predicate is passed, once behaves exactly as before. The predicate is validated up-front and will throw a TypeError if it’s not a function. Four new tests cover:

• resolving only on the first matching event
• throwing on invalid predicate types
• using multiple event names
• unsubscribing via promise.off() before a match

Closes #122

@sindresorhus
Copy link
Owner

You need to update the readme and index.d.ts

@faizanu94
Copy link
Contributor Author

faizanu94 commented Jun 19, 2025

@sindresorhus I’ve updated the readme and index.d.ts accordingly

@sindresorhus sindresorhus merged commit 347c2cc into sindresorhus:main Jun 20, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

once: support filter predicate

2 participants