Merge pull request #1368 from WhiskeySockets/fix-catch-unhandled-timeout-error

This commit is contained in:
Cassio Santos
2025-05-01 11:55:56 -03:00
committed by GitHub

View File

@@ -187,7 +187,7 @@ export const makeSocket = (config: SocketConfig) => {
let onRecv: (json) => void
let onErr: (err) => void
try {
return await promiseTimeout<T>(timeoutMs,
const result = await promiseTimeout<T>(timeoutMs,
(resolve, reject) => {
onRecv = resolve
onErr = err => {
@@ -199,6 +199,8 @@ export const makeSocket = (config: SocketConfig) => {
ws.off('error', onErr)
},
)
return result as any
} finally {
ws.off(`TAG:${msgId}`, onRecv!)
ws.off('close', onErr!) // if the socket closes, you'll never receive the message
@@ -213,11 +215,12 @@ export const makeSocket = (config: SocketConfig) => {
}
const msgId = node.attrs.id
const wait = waitForMessage(msgId, timeoutMs)
await sendNode(node)
const [result] = await Promise.all([
waitForMessage(msgId, timeoutMs),
sendNode(node)
])
const result = await (wait as Promise<BinaryNode>)
if('tag' in result) {
assertNodeErrorFree(result)
}