Skip to content

Commit

Permalink
feat: Allow close method to be used with Promises or callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Graziano Grespan committed Aug 16, 2024
1 parent 629d76f commit 7db5698
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,16 +669,27 @@ class ModbusRTU extends EventEmitter {
*
* @param {Function} callback the function to call next on close success
* or failure.
*/
*/
close(callback) {
// close the serial port if exist
if (this._port) {
this._port.removeAllListeners("data");
this._port.close(callback);
} else {
// nothing needed to be done
callback();
}
return new Promise((resolve, reject) => {
const cb = (err) => {
if (callback) {
callback(err);
}
if (err) {
reject(err);
} else {
resolve();
}
};

Check failure on line 685 in index.js

View workflow job for this annotation

GitHub Actions / test (18.x)

Trailing spaces not allowed.

Check failure on line 685 in index.js

View workflow job for this annotation

GitHub Actions / test (20.x)

Trailing spaces not allowed.
if (this._port) {
this._port.removeAllListeners("data");
this._port.close(cb);
} else {
cb();
}
});
}

/**
Expand Down

0 comments on commit 7db5698

Please sign in to comment.