safeEmitAsPromise in Transport.produce

What’s the reason for using safeEmitAsPromise in Transport.produce (and also in Transport.produceData)? Suppose a ‘produce’ event listener throws an error (as it was in my case - just a syntax error in JS). Then safeEmit catches the error, buries it in the debug log, and the caller waits forever, knowing nothing about the error. I’d prefer that the whole promise returned by Transport.produce was rejected.

Try this and recompile (npm run typescript:build):

diff --git a/src/EnhancedEventEmitter.ts b/src/EnhancedEventEmitter.ts
index f79fccf..ad0e581 100644
--- a/src/EnhancedEventEmitter.ts
+++ b/src/EnhancedEventEmitter.ts
@@ -31,8 +31,16 @@ export class EnhancedEventEmitter extends EventEmitter
 
 	async safeEmitAsPromise(event: string, ...args: any[]): Promise<any>
 	{
-		return new Promise((resolve, reject) => (
-			this.safeEmit(event, ...args, resolve, reject)
-		));
+		return new Promise((resolve, reject) =>
+		{
+			try
+			{
+				this.emit(event, ...args, resolve, reject);
+			}
+			catch (error)
+			{
+				reject(error);
+			}
+		});
 	}
 }

However, if you run async code in your listener and it throws, things are not gonna work.

BTW the idea is that you control your code, so if any error happens in your event listener you invoke the given reject(error) callback with it.

This will not work, because all exceptions from listeners are caught in safeEmit. Probably another method has to be added, say, emitAsPromise, that will do just this, but directly call emit instead of safeEmit.

I understand that the users of the library are expected to control their code, but errare humanum est, and even to start working on errors, one should at least know that the errors are there. It just natural to expect that the library propagates errors that are unrelated to the library itself.

right, fixed in my diff above, try it please.

Thank you, it works perfectly.

new version released BTW