-
Notifications
You must be signed in to change notification settings - Fork 189
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
Do not call the callback function twice when image size detection failed #25
Conversation
@netroy What do you think? |
try { | ||
callback(null, lookup(buffer, filepath)); |
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.
since lookup
is a sync function, only one of the callback methods would be ever called...
can you add a test that is broken before this change & fixed after 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.
OK, I'll do it. Done. #25 (comment)
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.
ah, took my slow brain a while to understand... Thanks for doing this.
Essentially we are handling errors from the callback function itself, which kinda feels dirty (handling someone else's errors)..
would it make sense if I change this to be something like this
var dimensions, err;
try {
dimensions = lookup(buffer, filepath);
} catch (e) {
err = e;
}
callback(err, dimensions);
solves the same issue without handling external errors.
PS: sorry for not being responsive on this PR, been crazy busy lately :(
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.
err
has been already defined here.
We need not to define it again.
var dimensions;
try {
dimensions = lookup(buffer, filepath);
} catch (e) {
err = e;
}
callback(err, dimensions);
|
Fixed. |
thanks for doing this.. |
Do not call the callback function twice when image size detection failed
You're welcome :) |
Currently image-size calls the callback function twice when the function throws an error.
This causes a confusion. When the callback function itself fails though size detection succeed, image-size tries to run the callback again.
For example, this code shows the log twice, unfortunately.
And besides, it causes a problem on asynchronous iteration using async or each-async etc.
For example,
When we run the code above, we expect an error message such as
Assertion errror: 0 != 100
.However, the code throws an iteration error like
Callback already called.
instead of assertion error, because the callback function is called twice.This PR fixes these problem. When the callback function is called, image-size will finish immediately.