1

The Chrome Extension Notification API has a create method with the following signature:

notifications.create(string notificationId, object options, function callback)

I don't actually have a callback that I need to run when the notification finishes, but if I omit the callback, it throws an error:

Error in response to storage.get: Error: Invocation of form notifications.create(string, object, null) doesn't match definition

Fair enough. I don't write a lot of JavaScript. What's the best way to use this API when I want a noop callback?

mlissner
  • 17,359
  • 18
  • 106
  • 169
  • 2
    Pass an empty function? `notifications.create("idHere", {options:here}, function() {});` – nnnnnn Jan 17 '15 at 03:25
  • 1
    Excellent. That's what I was thinking, but figured I better make sure that wasn't a more idiomatic approach. – mlissner Jan 17 '15 at 03:26
  • If the API didn't provide anyway to do the same thing without passing in a callback then you're stuck getting called back. It's not a big deal. If it was a string you didn't want to print you'd pass "". In the object oriented world you'd use the null object pattern and pass an object that does nothing when it's methods are called. – candied_orange Jan 17 '15 at 03:30
  • Iirc, with some of the other chrome apis, you can just leave out the third argument: `notifications.create ("idHere", object)`. – Teepeemm Jan 17 '15 at 04:55
  • 1
    Of note: there is a Chrome bug for this, but it's not updated in ages: https://code.google.com/p/chromium/issues/detail?id=163750 – Xan Jan 17 '15 at 12:12

1 Answers1

2

If the API forces you to pass some function then you can just pass an empty anonymous function:

notifications.create("idHere", {options:"something"}, function() {});

Or (especially if you need to do it in multiple places) you could explicitly define your own no-op function:

function noop() {}

notifications.create("idHere", {options:"something"}, noop);
someOtherAPI.someMethod(noop);
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • I like this except for calling the function `noop()`. If he's doing this in multiple places the benefit of having the explicitly defined function is that when it is discovered that something actually does need to be done on the callback you only have to put code in one place. However, once you do that the name `noop()` becomes a lie. `notificationCallback()` isn't perfect but it's better than `noop()`. – candied_orange Jan 17 '15 at 03:44
  • 1
    @CandiedOrange - I'd recommend `noop()` where the OP actually *wants* a no-op to take place. If `noop()` is used in multiple places and then the requirement changes, what are the chances that *all* of those places would have the same new generic requirement? (And if they did literally *all* change to the same new requirement, well, that's why my IDE has find/replace.) – nnnnnn Jan 17 '15 at 03:49
  • You give them the same name when they would have the same new requirement. This is an old idea showing up in a new light. The `noop()` you're recommending here is the functional version of a magic number. The name implies it should be used anytime you want to do nothing. Spread that widely in a large project and you're causing a maintenance headache. I humbly urge you to reach for a better name that reflects the context. – candied_orange Jan 17 '15 at 04:00
  • "Do nothing" is hardly a magic number. If it were my code as soon as I found a place where "do nothing" was no longer appropriate I would add a completely new function with an appropriate name, leaving `noop` for any other places that are still no-ops. At no time would I ever change a `noop()` function to have a non-empty body, other than perhaps a comment `/* Do nothing - do not change */`. (Having said that, I don't recall *ever* having needed a no-op function on a JavaScript project - the APIs I've used have all happily accepted `null` or `undefined` for a callback.) – nnnnnn Jan 17 '15 at 04:16