DEV Community

Discussion on: Explain Callback Function In JavaScript Like You Are 5 Years Old

willsmart profile image

There's a book mail-order company with pretty old systems.
You send a letter (snail-mail) to them saying which book you want to buy, and they send you back a letter saying when to come to the warehouse and pick it up (only pickups are allowed, they aren't really set up to send books by courier yet).

But they do give you one other option, which goes like this:
When sending them your letter saying which book you want, you're allowed to slip a phone into the envelope too.
When the book is ready, they'll power up the phone, find the app called "book-callback", and open it, and type some details about the book.

So to have your book sent to you, write a little app called "book-callback" that can book a courier to send something to your house, put your app on a cheapo phone, and send that in the envelope.
So, even though the book company doesn't know anything about couriers (or even where you live), you still get your book in the post.

A callback in programming is like that phone. It's a bit of your own code you can send in a request. The one doing the work doesn't need to know how to it all, just how to kick off your callback. Your code takes it from there.