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

This lesson ports two callbacks: the class’s full-up warning and its end-of-class reminder. The Learn to Boulder class has two places. Python’s make_session() takes an optional callback for when it fills, on_full, None by default, and checks that one was passed before calling it with the class’s name:

Language: Python
def make_session(
    name,
    prefix,
    places,
    on_full=None,
):
    climbers = []
    def check_in(climber):
        climbers.append(climber)
        if len(climbers) == places and on_full is not None:
            on_full(name)
        return f"{prefix}-{100 + len(climbers)} {climber}"
    return check_in

def announce_full(name):
    print(f"{name} is full")

intro = make_session(
    "Learn to Boulder", "L", 2, on_full=announce_full
)
print(intro("Mia Novak"))
print(intro("Leo Park"))

Run it for Mia and Leo, and Leo’s warning comes before his wristband. What does JavaScript do instead of that check?

In JavaScript, the factory ports line by line, with camelCase names, returning the check-in as an arrow. The list becomes an array, and the f-string a template literal. The callback becomes a short arrow, as small functions usually are. With no keyword arguments, it travels in an options object, keyed by name: onFull. The last two lines log the wristbands.

Where Python defaults to None, JavaScript usually gives an optional callback a do-nothing default: an arrow with an empty body, () => {}. The outer braces pull onFull out of the options object, and the = {} after them covers a call without one. So the check drops out:

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 two callbacks: the class’s full-up warning and its end-of-class reminder. The Learn to Boulder class has two places. Python’s make_session() takes an optional callback for when it fills, None by default, and checks that one was passed before calling it with the class’s name.

00:20 Run it for Mia and Leo, and Leo’s warning comes before his wristband. What does JavaScript do instead of that check?

00:30 In JavaScript, the factory ports line by line, with camelCase names, returning the check-in as an arrow. The list becomes an array, and the f-string a template literal.

00:43 The callback becomes a short arrow, as small functions usually are. With no keyword arguments, it travels in an options object, keyed by name. The last two lines log the wristbands.

00:58 Where Python defaults to None, JavaScript usually gives an optional callback a do-nothing default: an arrow with an empty body. The outer braces pull onFull out of the options object, and the empty-object default after them covers a call without one.

01:15 So the check drops out, and the check-in calls the callback without asking. With no option, the empty arrow runs, and nothing happens.

01:27 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.

01:39 Here, that’s once, when the last place goes, with the class’s name.

01:45 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:58 It prints Reminder set, then the reminder itself.

02:03 JavaScript’s timer is the global setTimeout(), with nothing to import. It takes the callback first, then the delay, and starts at once: no .start() call.

02:13 Leave it on: the file stops with a TypeError: .start is not a function.

02:21 Run the port, and it logs Reminder set. When does the reminder itself appear? Your turn: pause the video and predict.

02:36 Almost at once. It turns up 0.6 seconds later, nearly ten minutes early, with no error.

02:46 JavaScript counts the delay in milliseconds. So 600 means 0.6 of a second, where Python’s Timer, like time.sleep(), counts in seconds.

02:59 The fix: spell the delay out in milliseconds, ten minutes, times sixty seconds, times a thousand milliseconds. Run it again: the reminder comes at twenty past eight.

03:13 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.

03:25 Now give the reminder the class’s name. Python uses 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.

03:42 setTimeout() calls the arrow, and the arrow calls remind() with the name. Like any function, the arrow is a closure: it can use remind(), or any variable around it.

03:53 Run it, and the reminder still comes ten minutes later.

03:59 Pass a callback, and the function that receives it calls it when the moment comes, as .map() and .reduce() do. Give an optional one a do-nothing default, and a timer its delay in milliseconds, spelled out.

04:13 Next up is Building Functions From Functions, a text lesson. For student night, you’ll port a function that builds other functions.

Become a Member to join the conversation.