Repository: discordjs/discord.js
File: packages/ws/src/ws/WebSocketShard.ts (TypeScript)

Code before:

		if (this.connection) {
			// No longer need to listen to messages
			this.connection.onmessage = null;
			// Prevent a reconnection loop by unbinding the main close event
			this.connection.onclose = null;

			const shouldClose = this.connection.readyState === WebSocket.OPEN;

			this.debug([
				'Connection status during destroy',
				`Needs closing: ${shouldClose}`,
				`Ready state: ${this.connection.readyState}`,
			]);

			if (shouldClose) {
				this.connection.close(options.code, options.reason);

				let outerResolve: () => void;
				const promise = new Promise<void>((resolve) => {
					outerResolve = resolve;
				});

				this.connection.onclose = () => {
					outerResolve();
				};

				await promise;
				this.emit(WebSocketShardEvents.Closed, { code: options.code });
			}

			// Lastly, remove the error event.
			// Doing this earlier would cause a hard crash in case an error event fired on our `close` call
			this.connection.onerror = null;
		} else {
			this.debug(['Destroying a shard that has no connection; please open an issue on GitHub']);
		}

		this.#status = WebSocketShardStatus.Idle;

		if (options.recover !== undefined) {
			// There's cases (like no internet connection) where we immediately fail to connect,
			// causing a very fast and draining reconnection loop.
			await sleep(500);
			return this.internalConnect();
		}
	}

	private async waitForEvent(event: WebSocketShardEvents, timeoutDuration?: number | null): Promise<{ ok: boolean }> {
		this.debug([`Waiting for event ${event} ${timeoutDuration ? `for ${timeoutDuration}ms` : 'indefinitely'}`]);
		const timeoutController = new AbortController();

Review comment (suggestion):
Wouldn't it be cleaner to do this?
```suggestion
				this.connection.onclose = outerResolve;
```

Code after:

		if (this.connection) {
			// No longer need to listen to messages
			this.connection.onmessage = null;
			// Prevent a reconnection loop by unbinding the main close event
			this.connection.onclose = null;

			const shouldClose = this.connection.readyState === WebSocket.OPEN;

			this.debug([
				'Connection status during destroy',
				`Needs closing: ${shouldClose}`,
				`Ready state: ${this.connection.readyState}`,
			]);

			if (shouldClose) {
				let outerResolve: () => void;
				const promise = new Promise<void>((resolve) => {
					outerResolve = resolve;
				});

				this.connection.onclose = outerResolve!;

				this.connection.close(options.code, options.reason);

				await promise;
				this.emit(WebSocketShardEvents.Closed, { code: options.code });
			}

			// Lastly, remove the error event.
			// Doing this earlier would cause a hard crash in case an error event fired on our `close` call
			this.connection.onerror = null;
		} else {
			this.debug(['Destroying a shard that has no connection; please open an issue on GitHub']);
		}

		this.#status = WebSocketShardStatus.Idle;

		if (options.recover !== undefined) {
			// There's cases (like no internet connection) where we immediately fail to connect,
			// causing a very fast and draining reconnection loop.
			await sleep(500);
			return this.internalConnect();
		}
	}

	private async waitForEvent(event: WebSocketShardEvents, timeoutDuration?: number | null): Promise<{ ok: boolean }> {
		this.debug([`Waiting for event ${event} ${timeoutDuration ? `for ${timeoutDuration}ms` : 'indefinitely'}`]);
		const timeoutController = new AbortController();
		const timeout = timeoutDuration ? setTimeout(() => timeoutController.abort(), timeoutDuration).unref() : null;