Don’t use functions as callbacks unless they’re designed for it

Solid advice by Jake:

Here’s an old pattern that seems to be making a comeback:

import { toReadableNumber } from 'some-library';
const readableNumbers = someNumbers.map(toReadableNumber);

Here’s the problem:

// We think of:
const readableNumbers = someNumbers.map(toReadableNumber);
// …as being like:
const readableNumbers = someNumbers.map((n) => toReadableNumber(n));
// …but it's more like:
const readableNumbers = someNumbers.map((item, index, arr) =>
  toReadableNumber(item, index, arr),
);

You might have encountered this yourself when combining map with parseInt, as its second parameter defines the base to use 😅

Don’t use functions as callbacks unless they’re designed for it →

map is one of the array methods I use a lot, along with filter and reduce. This post right here explains them using a simple example.

Demystifying jQuery 1.7′s $.Callbacks

$.Callbacks provides a way to manage lists of callbacks and it’s actually quite powerful. If we were to define two functions fn1 and fn2 we can then add these functions as callbacks to a $.Callbacks list and invoke them via the fire method. The result of this is that it becomes simple to construct complex lists of callbacks where input values can be passed through to as many functions as needed with ease.

Demystifying jQuery 1.7′s $.Callbacks →