Node handling Primus socket disconnect during substream write

202 Views Asked by At

If I have a (synchronous) process writing to a substream on a socket which disconnects during code execution, what's the best way to keep it from throwing the exception?

(I'm listening to the socket's close/end/etc events to remove the block from flow, but these event handlers wouldn't fire until after the code has finished)

Should I do this:

if (clientStream.stream) {
  clientStream.write(bufferData);
} else {
  console.log('*** Client FAILURE *****')
}

or use a try/catch?

try {
  clientStream.write(bufferData);
} catch (err) {
  console.log('*** Client FAILURE *****')
}

I know try/catches are expensive, but I haven't found any info on checking the clientStream.stream object to verify it exists. Maybe it's been deprecated like stream.readyState?

0

There are 0 best solutions below