chore: avoid unhandled promise timeout errors

This commit is contained in:
canove
2025-04-24 13:37:03 -03:00
parent 9149e58f78
commit 2382fb8723

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,13 @@ 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),
await sendNode(node)
])
const result = await (wait as Promise<BinaryNode>)
if('tag' in result) {
assertNodeErrorFree(result)
}