Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Calling Back When Something Happens

Calling Back When Something Happens

You’ll port the Learn to Boulder class’s two callbacks: a warning when its two places fill, and a reminder before it ends. Python’s make_session() defaults the warning callback to None and checks on_full is not None before calling it. What does JavaScript do instead of that check?

In JavaScript, the factory and its warning port line by line; with no keyword arguments, the warning travels in an options object. Where Python uses None, JavaScript usually gives an optional callback a do-nothing default: an empty arrow, () => {}. The outer braces pull onFull out of the options, which default to an empty object, = {}. So the check drops out, and the check-in calls onFull(name) without asking:

Language: JavaScript
function makeSession(
  name,
  prefix,
  places,
  { onFull = () => {} } = {},
) {
  const climbers = [];
  return (climber) => {
    climbers.push(climber);
    if (climbers.length === places) {
      onFull(name);
    }
    return `${prefix}-${100 + climbers.length} ${climber}`;
  };
}

const announceFull = (name) =>
  console.log(`${name} is full`);

const intro = makeSession("Learn to Boulder", "L", 2, {
  onFull: announceFull,
});
console.log(intro("Mia Novak"));
console.log(intro("Leo Park"));

With no option, the empty arrow runs, and nothing happens. Run the port, and Leo’s warning lands before his wristband, as in Python:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 You’ll port the Learn to Boulder class’s two callbacks: a warning when it fills, and a reminder before it ends. Python’s make_session() defaults the warning callback to None and checks for it.

00:11 What does JavaScript do instead of that check?

00:16 The factory and its warning port line by line; with no keyword arguments, the warning travels in an options object. Where Python uses None, JavaScript usually gives an optional callback a do-nothing default: an empty arrow.

00:32 The outer braces pull onFull out of the options, which default to an empty object. So the check drops out: with no option, the empty arrow runs, doing nothing.

00:45 Run the port, and Leo’s warning lands before his wristband, as in Python. The function that receives a callback decides when to call it, how often, and with what: here, once, when the last place goes.

01:00 Timers take callbacks too. At ten past eight, the desk sets a ten-minute reminder: Python’s threading.Timer takes the delay and the function, and .start() sets it going.

01:11 It prints Reminder set, then the reminder.

01:15 JavaScript’s global setTimeout() needs no import, takes the callback first, then the delay, and starts at once: no .start() call.

01:24 Leave it on, and the file stops with a TypeError. Run the port: it logs Reminder set. When does the reminder appear? Your turn: pause the video and predict.

01:43 Almost at once. It comes 0.6 seconds later, nearly ten minutes early, with no error.

01:53 JavaScript counts the delay in milliseconds: 600 is 0.6 of a second, where Python’s Timer counts seconds. The fix: ten minutes, times sixty seconds, times a thousand milliseconds.

02:08 Run it: it comes at twenty past eight. Both print Reminder set first, but Python’s timer thread can cut in mid-script, while JavaScript always finishes the file first, even with a delay of zero.

02:24 To hand the reminder the class’s name, Python might reach for functools.partial, as here. JavaScript has no functools. The everyday way is to wrap the call in an arrow, as a Python lambda with no parameters would.

02:39 The arrow is a closure: it can use any variable around it. Run it: the reminder still comes ten minutes later.

02:49 Pass a callback, and the function that receives it calls it when the moment comes. Give an optional one a do-nothing default, and a timer its delay in milliseconds.

02:59 Next up: Building Functions From Functions. For student night, you’ll port a function that builds other functions.

Become a Member to join the conversation.