Skip to content

Instantly share code, notes, and snippets.

@AlexFrazer
Last active March 28, 2016 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexFrazer/3315ef1cebfb198d00d5 to your computer and use it in GitHub Desktop.
Save AlexFrazer/3315ef1cebfb198d00d5 to your computer and use it in GitHub Desktop.
class ExtendedSocket extends Socket {
// ...
// need to use this to get the error response.
tellErrorCode() {
return new Promise((resolve, reject) => {
this.once('data', data => {
resolve(new Error('CommandError', data.replace(/\:\r\n/, '')));
}).write('TC 1\r\n'); // asks for the error
}).timeout(5000);
}
send(command) {
let listener;
return new Promise((resolve, reject) => {
listener = data => {
data.split(/\r\n/).forEach(line => {
if (/^\:$/.test(line)) resolve();
if (/^\?$/.test(line)) reject(); // need to tell error code here
})
}
this.on('data', listener).write(`${command}\r\n`);
}).timeout(5000).finally(() => {
this.removeListener('data', listener);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment