How to explain callbacks in plain English? How are they different from calling one function from another function?
-
How to explain callbacks in plain English? How are they different from calling one function from another function taking some context from the calling function? How can their power be explained to a novice programmer?
-
Answer:
Simply calling one function is the case that the caller F knows some function G that it can call when it needs to. But callback is the case that the caller F will be given some function G at run time, which, as long as has the right signature, can be called. That is to say, in the latter case, F needs not to know which function it will call at the time it was written, and any function with the right signature can be given to F and get called.
Yu Feng at Quora Visit the source
Other answers
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.
Sandro Pasquali
Imagine you had a warehouse, and a robot that would go looking for things in that warehouse. The robot is not built for moving the things it finds, mind, but it is really good at finding certain objects inside the warehouse, when told to go searching. This robot also has friends that have a similar build, like one that finds good spaces for objects, and maybe figuring out how whole shelves could be reorganized, but like the first, doesn't actually move them. These are the callback functions. These callback robots, at their digression, may also call other robots to do their bidding. These other robots do move items about, but each one does it differently. One has a forklift, one has a crane, maybe one has a big 'ol hydraulic lift that moves whole shelves, it doesn't matter. These robots that actually do the work are the called functions of the original callback function. The original callback robot (function) will go about figuring things out, and then it will call the other robots (functions) at its disposal. One of the more common callback functions one may see are list operations. The callback function search(), for example, will find the object(s) that meet the stated specifications, then search() will call the function passed to it on all found entries. This function is usually some sort of data operation, like tabulate() or delete() or even overwrite(). Search() has been written in a general enough fashion to be reused for anything in this list, but as for what actually happens to the found things are left to later programmers. This is the power of callbacks. The callback function gets another function to the right place, and then the actual operation is initiated.
Westin Breger
A function has a set of instructions to carry out. One of the instructions is call a function F, wait for it to finish, take its output, do some processing on the output and then carry on with the remaining set of instructions. In this scenario we have to wait for the function F to finish its processing and only then the original function carries out the remaining instructions. With Callbacks. the Function F runs in parallel with the original function. the original function , gives a "callback function" to F. When ever the function F finishes its processing, it calls this callback function. The original function then stops what it was doing, takes the data given by the function F ,using the "callback function", process it and then carries on with its work.
Vinayendra Chavan
This should help https://knownodejs.quora.com/Callback-functions
Lovy Gupta
Related Q & A:
- How to access a nested function from another nested function in javascript?Best solution by devarticles.com
- How to pass an ArrayList from one class to another?Best solution by stackoverflow.com
- How can I move bookmarks from one email to another one?Best solution by pcworld.com
- How do you transfer info from one account to another in Yahoo?Best solution by Yahoo! Answers
- How can I transfer contacts from one phone to another?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.