Skip to content

Commit

Permalink
do not continue
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT committed Nov 29, 2024
1 parent e3052e3 commit 0d0b62e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
9 changes: 5 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,16 +509,17 @@ export default class Emittery<
/**
Same as `emit()`, but it waits for each listener to resolve before triggering the next one. This can be useful if your events depend on each other. Although ideally they should not. Prefer `emit()` whenever possible.
If any of the listeners throw/reject, the remaining listeners will *not* be called.
@returns A promise that resolves when all event listeners are done:
- If all listeners succeed, resolves with undefined
- If any listeners fail, resolves with an array of their errors
All listeners will execute regardless of whether other listeners throw/reject.
- If any listeners fail, resolves with an array with exactly one error
*/
emitSerial<Name extends DatalessEvents>(eventName: Name): Promise<EmitResult>;
emitSerial<Name extends DatalessEvents>(eventName: Name): Promise<[unknown]>;
emitSerial<Name extends keyof EventData>(
eventName: Name,
eventData: EventData[Name]
): Promise<EmitResult>;
): Promise<[unknown]>;

/**
Subscribe to be notified about any event.
Expand Down
10 changes: 2 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,15 +408,13 @@ export default class Emittery {

await resolvedPromise;

const errors = [];

/* eslint-disable no-await-in-loop */
for (const listener of staticListeners) {
if (listeners.has(listener)) {
try {
await listener(eventData);
} catch (error) {
errors.push(error);
return [error];
}
}
}
Expand All @@ -426,16 +424,12 @@ export default class Emittery {
try {
await listener(eventName, eventData);
} catch (error) {
errors.push(error);
return [error];
}
}
}
/* eslint-enable no-await-in-loop */

if (errors.length > 0) {
return errors;
}

return undefined;
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@
"lcov",
"text"
]
}
},
"packageManager": "pnpm@9.12.3+sha512.cce0f9de9c5a7c95bef944169cc5dfe8741abfb145078c0d508b868056848a87c81e626246cb60967cbd7fd29a6c062ef73ff840d96b3c86c40ac92cf4a813ee"
}
5 changes: 2 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ test('emitSerial() - returns undefined when all listeners succeed', async t => {
t.is(result, undefined);
});

test('emitSerial() - returns array of errors when listeners fail', async t => {
test('emitSerial() - returns array of the error when listeners fail', async t => {
const emitter = new Emittery();
const syncError = new Error('sync error');
const asyncError = new Error('async error');
Expand All @@ -1373,7 +1373,6 @@ test('emitSerial() - returns array of errors when listeners fail', async t => {

const result = await emitter.emitSerial('test', 'data');
t.true(Array.isArray(result));
t.is(result.length, 2);
t.is(result.length, 1);
t.is(result[0], syncError);
t.is(result[1], asyncError);
});

0 comments on commit 0d0b62e

Please sign in to comment.