I rarely dip into sports analogies to explain web development concepts, but this one is the real MVP. 😜
A play in football is a sequence of actions executed in basically the same way every time. One of the most famous plays is the “Hail Mary.” I’m not much for football, but, as I understand the play, all the receivers run as fast as they can toward the end zone and the quarterback throws as far as he can to one of them.
The “Hail Mary” is a simple play, but plays can be much more complex. If you have a complicated play with multiple steps and decisions to be made as it progresses, it’s much easier if the coach and the players have a simple way to refer to it. That makes it easy to call it over and over without having to list out all 15 steps every single time.
Imagine a football coach discovers one of the team’s plays doesn’t work very well against an upcoming opponent and wants to modify the play. They could now, instead of calling the old play, call for a time-out each time they want to run the play and explain all the new steps. That might work, but it would be pretty cumbersome.
Instead, they could tell all the players about the modifications to the play and update the playbook at practice before the game. This would allow them to call the same play by the same name but get the new play execution.
The coach has the flu and will miss the next game. How can we allow the assistant coach to use our existing plays? We can either pass along the playbook, or we can rip out individual plays to share. Since they are written down and named, they’re easy to pass around.
One way to do this is by passing a function into your asynchronous function call. When the asynchronous operation is done, it will call the function you passed to it. It’s kinda like sharing a play for someone else to try later. There are other cases where you’ll use functions like this (often called “callback functions”), but this is an example you’ll use often.