-
-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make emit()
wait for all listeners to settle
#51
Comments
I think it is a bad idea, for And so I implemented it here, where for the list of all recipients I execute a regular try {
const res = sub.cb?.(data);
if (typeof res?.catch === 'function') {
res.catch((err: any) => onError(err, sub.name));
}
} catch (e) {
onError(e, sub.name);
} This way my Other than that, the idea of making |
Are the requirements for this finalised? There was some discussion on that PR about whether const goodHandler = () => Promise.resolve(123)
const badHandler = () => Promise.reject(456)
const e1 = new Emittery() // no error event name declared - `.emit()` will reject when a handler throws as before
const e2 = new Emittery({ error: 'error' }) // explicitly declare what event should be used for errors. Solves the 'error' string vs Symbol debate too
e1.on('foo', goodHandler)
e2.on('foo', goodHandler)
e1.on('foo', badHandler)
e2.on('foo', badHandler)
await e1.emit() // throws WrappedError([{ status: 'fulfilled', value: 123 }, { status: 'rejected', reason: 456 }])
await e2.emit() // doesn't throw, but emits 'error' event WrappedError as above Implementation-wise, this seems a good candidate for Promise.allSettled (or a polyfill since this package supports node 10) - which is where the |
From the linked PR:
This is what I think we should do. |
Hey, I just found this old thread while thinking of adopting emittery. Would it make sense for import Emittery from 'emittery';
const emitter = new Emittery<{
'🦄': string;
}>();
emitter.on('🦄', () => {
// works
});
emitter.on('🦄', () => {
throw new Error('💣');
});
const result = await emitter.emit('🦄', 'a');
if (result) {
// result would be [Error('💣')]
console.error('Errors', result);
} else {
console.log('all OK!');
} |
See the initial attempt and feedback in #19.
The text was updated successfully, but these errors were encountered: