How to explain callbacks in plain English? How are they different from calling one function...

Quora Feeds

Active Member
Sandro Pasquali

Everybody has bought food over a counter. At McDonalds, I tell someone "I want a Big Mac", establish my credentials by giving them money, then move aside while I wait for my food. When my food is ready, I am called, and receive what I asked for.

Everybody has taken money from an ATM. I assert that I want some money, I establish my credentials with a PIN, wait, and (usually) get that amount of money.

The key difference between those two scenarios is that in the first the next customer can be served while I wait for my food. In the second, those who want to use the ATM must wait until I'm done.

The first uses callbacks: Any number of requests are made, and as requests are fulfilled, the caller (customer) is notified and provided with what they asked for.

The second is procedural: One request is made, one request is fulfilled, other requests must wait their turn.

The first allows multiple orders to be taken at the same time (parallel). The second does not (sequential).

Asynchronous systems are able to fulfill many more requests per unit of time than synchronous systems.

Real world example: Google.

It should be obvious that "all the world's data" is too much data to be stored on one storage device. When you make a request for "asynchronous programming" many devices must be searched.

Google operates a very large number (hundreds of thousands) of storage devices. Let's say there are only 100. On these devices is stored the data Google uses to fulfill search requests.

Let's say it takes one second to search one storage device.

One way for Google to perform a search would be to ask one worker to search one device, then ask another worker to search his device, and so on, repeating about 100 times -- asking for 100 search functions to be performed in sequence. This would take 100 seconds, which is a long time to wait (almost two minutes).

What Google actually does is ask each of 100 workers to check one of the storage devices. The 100 search functions are done in parallel. The total time to check 100 is the time it takes to check one(1). That is, it takes one second.

The general asynchronous technique Google is using here is called "Map/Reduce", which you may have heard of, and is worth some study. Search for it on Google.

See Questions On Quora

Continue reading...
 
Top