-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent unfulfilled promises on sendCommand method #22
base: master
Are you sure you want to change the base?
Conversation
@@ -275,27 +275,26 @@ export default class Client { | |||
|
|||
// sendCommand :: Command -> Number -> Promise Command | |||
sendCommand(command, timeout = this._application.commandTimeout) { | |||
var commandPromise = Promise.race([ | |||
const commandPromise = Promise.race([ | |||
new Promise((resolve, reject) => { | |||
this._commandResolves[command.id] = (c) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent a memory leak caused by an error in this function execution, you can design it in that way:
() => {
try {
// logic here
} catch (err) {
return reject(...);
} finally {
delete this._commandResolves[command.id];
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually need a race of 2 promises here, because the command must fail if there was no response before the timeout
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't question that. I'm talking about the arrow function you pass on the first promise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The try/catch block is a good way to prevent unhandled errors. By the way, it might help us to handle exceptions better at BLiP Portal
setTimeout(() => { | ||
if (!this._commandResolves[command.id]) | ||
return; | ||
if (!this._commandResolves[command.id]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need to check it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just added the bracket, but this if is used to guarantee the promise will only be reject for commands with timeout
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That if statement seems to be impossible to be true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But anyway, I don't think that returning a string can be a good thing, think that consumers are expecting a specific type, in the case a lime envelope, returning a different type can cause problems. Miss you, typescript :(
Prevent keeping unfulfilled promises on memory because of SendCommand method.