# Node.js callbacks get called multiple times when they throw an exception

**URL:** <https://forums.blinkstick.com/t/node-js-callbacks-get-called-multiple-times-when-they-throw-an-exception/151>\
**Category:** Support\
**Created:** [March 11, 2015, 7:11pm UTC](https://forums.blinkstick.com/t/node-js-callbacks-get-called-multiple-times-when-they-throw-an-exception/151 "2015-03-11T19:11:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SkyLined](https://forums.blinkstick.com/user_avatar/forums.blinkstick.com/skylined/32/90_2.png) [@SkyLined](https://forums.blinkstick.com/u/SkyLined)\
**Post date:** [March 11, 2015, 7:11pm UTC](https://forums.blinkstick.com/t/node-js-callbacks-get-called-multiple-times-when-they-throw-an-exception/151/1 "2015-03-11T19:11:25Z")

</div>

It appears the callbacks are called from inside a try … catch block, and any exception they throw will result in more calls to the callback.

```auto
var mBlinkStick = require("blinkstick"),
    oBlinkStick = mBlinkStick.findFirst();
oBlinkStick.setMode(2, function (oError) {
  if (oError) {
    console.log("callback with error:", oError);
  } else {
    console.log("callback without error");
    throw new Error("This should not cause another callback");
  }
});

```

Expected output:

```auto
H:\dev\node\pWinStatusBlinkStick>test2.js
callback without error
H:\dev\node\pWinStatusBlinkStick\test2.js:9
      throw new Error("This should not cause another callback");
            ^
Error: This should not cause another callback
    at ...

```

Actual output:

```auto
H:\dev\node\pWinStatusBlinkStick>test2.js
callback without error
callback without error
callback with error: [Error: This should not cause another callback]

```

I believe you can resolve this in your code by wrapping the callbacks in a setTimeout(…, 0), by replacing this:

```auto
    fCallback(arg0, arg1, ...);

```

With this:

```auto
    setTimeout(function () {
        fCallback(arg0, arg1, ...);
    }, 0);

```

I’ll look into submitting another pull request for this issue tomorrow. Doing this should also resolve the stack exhaustion bug I reported earlier today.

---

<div class="post-metadata">

**Author:** ![SkyLined](https://forums.blinkstick.com/user_avatar/forums.blinkstick.com/skylined/32/90_2.png) [@SkyLined](https://forums.blinkstick.com/u/SkyLined)\
**Post date:** [March 11, 2015, 9:20pm UTC](https://forums.blinkstick.com/t/node-js-callbacks-get-called-multiple-times-when-they-throw-an-exception/151/2 "2015-03-11T21:20:02Z")

</div>

I’ve modified my local copy of BlinkStick’s Node.js code to use…

```auto
process.nextTick(function() {
  callback(...);
});

```

…rather than…

```auto
  callback(...);

```

…where appropriate: not all instances need replacing. (process.nextTick is similar to setTimout, but more appropriate).

This resolves the issue for me: no more double calls to the callback function that report an exception thrown there, but only one call followed by an uncaught exception. If you’ll let me know which branch to apply these changes to, I can get you a pull request for testing.
