netroy/image-size — pull request #25 by shinnn (2014-07-09 06:54:15+00:00)
Title: Do not call the callback function twice when image size detection failed

Currently [image-size calls the callback function twice](https://github.com/netroy/image-size/blob/c2a859f19eec993eecbea7b64f2858b7246d977b/lib/index.js#L83-L88) when the function throws an error.
This causes a confusion. When the callback function itself failed though the image size detection succeeded, image-size tries to run the callback again.

For example, it shows the log twice, unfortunately.

```javascript
sizeOf('img.jpg', function() {
console.log('This message should be shown only once!');
throw new Error();
})
```

```sh
> This message should be shown only once!
> This message should be shown only once!
```

And besides, it causes a problem on asynchronous iteration using [async](https://github.com/caolan/async) or [each-async](https://github.com/sindresorhus/each-async) etc.

For example,

```js
async.each(imagePathArray, function(imagePath, i, next) {
imageSize(imagePath, next);
}, function() {
assert.equal(0, 100); // it throws an error
});
```

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 should finish immediately.