* 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>>
)
| 385 | * @return a Promise that will be fulfilled when the client acknowledges the event |
| 386 | */ |
| 387 | public emitWithAck<Ev extends EventNamesWithAck<EmitEvents>>( |
| 388 | ev: Ev, |
| 389 | ...args: AllButLast<EventParams<EmitEvents, Ev>> |
| 390 | ): Promise<FirstNonErrorArg<Last<EventParams<EmitEvents, Ev>>>> { |
| 391 | // the timeout flag is optional |
| 392 | const withErr = this.flags.timeout !== undefined; |
| 393 | return new Promise((resolve, reject) => { |
| 394 | args.push((arg1, arg2) => { |
| 395 | if (withErr) { |
| 396 | return arg1 ? reject(arg1) : resolve(arg2); |
| 397 | } else { |
| 398 | return resolve(arg1); |
| 399 | } |
| 400 | }); |
| 401 | this.emit(ev, ...(args as any[] as EventParams<EmitEvents, Ev>)); |
| 402 | }); |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * @private |
no test coverage detected