* Emits an event and waits for an acknowledgement * * @example * io.on("connection", async (socket) => { * // without timeout * const response = await socket.emitWithAck("hello", "world"); * * // with a specific timeout * try { * const response = await socket.t
(
ev: Ev,
...args: AllButLast<EventParams<EmitEvents, Ev>>
)
| 292 | * @return a Promise that will be fulfilled when the client acknowledges the event |
| 293 | */ |
| 294 | public emitWithAck<Ev extends EventNamesWithAck<EmitEvents>>( |
| 295 | ev: Ev, |
| 296 | ...args: AllButLast<EventParams<EmitEvents, Ev>> |
| 297 | ): Promise<FirstNonErrorArg<Last<EventParams<EmitEvents, Ev>>>> { |
| 298 | // the timeout flag is optional |
| 299 | const withErr = this.flags.timeout !== undefined; |
| 300 | return new Promise((resolve, reject) => { |
| 301 | args.push((arg1, arg2) => { |
| 302 | if (withErr) { |
| 303 | return arg1 ? reject(arg1) : resolve(arg2); |
| 304 | } else { |
| 305 | return resolve(arg1); |
| 306 | } |
| 307 | }); |
| 308 | this.emit(ev, ...(args as any[] as EventParams<EmitEvents, Ev>)); |
| 309 | }); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * @private |
no test coverage detected