Questions tagged [callback]

A callback is a piece of code (i.e. the address or reference of a function or method or a lambda expression) that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. This tag should be used with questions about an API that uses call backs to notify the caller when an action is complete. Use the event-handling tag for questions involving subscribing to events such as in a GUI framework.

In computer programming, a callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. The invocation may be immediate as in a synchronous callback or it might happen at later time, as in an asynchronous callback.

Wikipedia article: http://en.wikipedia.org/wiki/Callback_(computer_programming)


An example of sync process callback in javascript:
function outer(argumentOne, fn){
    console.log("what was argumentOne? ", argumentOne);
    return fn();
}

function someCallback() {
    console.log("callback triggered!");
}

outer("brown", someCallback);

An example of async process callback in javascript:

function mySandwich(param1, param2, callback) {
        console.log('Started eating my sandwich.\n\n It has: ' + param1 + ', ' + param2);
    setTimeout(function(){
        callback(null,param1*param2);},2000);
    }

    mySandwich(1, 2, function(err,result) {
        console.log('Finished eating my sandwich.' + result);
    });

Output:

Started eating my sandwich.

It has: 1, 2
Finished eating my sandwich.2
18062 questions
169
votes
4 answers

Idiomatic callbacks in Rust

In C/C++ I'd normally do callbacks with a plain function pointer, maybe passing a void* userdata parameter too. Something like this: typedef void (*Callback)(); class Processor { public: void setCallback(Callback c) { mCallback =…
Timmmm
  • 88,195
  • 71
  • 364
  • 509
168
votes
11 answers

How should I call 3 functions in order to execute them one after the other?

If I need call this functions one after other, $('#art1').animate({'width':'1000px'},1000); $('#art2').animate({'width':'1000px'},1000); $('#art3').animate({'width':'1000px'},1000); I know in jQuery I could do something…
texai
  • 3,696
  • 6
  • 31
  • 41
164
votes
8 answers

Getting a better understanding of callback functions in JavaScript

I understand passing in a function to another function as a callback and having it execute, but I'm not understanding the best implementation to do that. I'm looking for a very basic example, like this: var myCallBackExample = { myFirstFunction…
user4903
164
votes
6 answers

How to "await" for a callback to return?

When using a simple callback such as in the example below: test() { api.on( 'someEvent', function( response ) { return response; }); } How can the function be changed to use async / await? Specifically, assuming 'someEvent' is guaranteed…
sean2078
  • 5,131
  • 6
  • 32
  • 32
159
votes
7 answers

How to Define Callbacks in Android?

During the most recent Google IO, there was a presentation about implementing restful client applications. Unfortunately, it was only a high level discussion with no source code of the implementation. In this diagram, on the return path there are…
user409841
  • 1,637
  • 2
  • 11
  • 5
156
votes
7 answers

Sort a list of lists with a custom compare function

I know there are several questions named like this, but they don't seem to work for me. I have a list of lists, 50 times 5 elements. I want to sort this list by applying a custom compare function to each element. This function calculates the fitness…
DaClown
  • 4,171
  • 6
  • 31
  • 31
147
votes
14 answers

jQuery callback for multiple ajax calls

I want to make three ajax calls in a click event. Each ajax call does a distinct operation and returns back data that is needed for a final callback. The calls themselves are not dependent on one another, they can all go at the same time, however I…
MisterIsaak
  • 3,882
  • 6
  • 32
  • 55
141
votes
6 answers

Executing async code on update of state with react-hooks

I have something like: const [loading, setLoading] = useState(false); ... setLoading(true); doSomething(); // <--- when here, loading is still false. Setting state is still async, so what's the best way to wait for this setLoading() call to be…
Colin Ricardo
  • 16,488
  • 11
  • 47
  • 80
140
votes
5 answers

How to use class methods as callbacks

I have a class with methods that I want to use as callbacks. How can I pass them as arguments? Class MyClass { public function myMethod() { // How should these be called? $this->processSomething(this->myCallback); …
SmxCde
  • 5,053
  • 7
  • 25
  • 45
140
votes
10 answers

nodeJs callbacks simple example

can any one give me a a simple example of nodeJs callbacks, I have already searched for the same on many websites but not able to understand it properly, Please give me a simple example. getDbFiles(store, function(files){ getCdnFiles(store,…
Bhushan Goel
  • 2,114
  • 3
  • 19
  • 31
139
votes
13 answers

Python time measure function

I want to create a python function to test the time spent in each function and print its name with its time, how i can print the function name and if there is another way to do so please tell me def measureTime(a): start = time.clock() a() …
Wazery
  • 15,394
  • 19
  • 63
  • 95
134
votes
8 answers

What is "callback hell" and how and why does RX solve it?

Can someone give a clear definition together with a simple example that explains what is a "callback hell" for someone who does not know JavaScript and node.js ? When (in what kind of settings) does the "callback hell problem" occur? Why does it…
jhegedus
  • 20,244
  • 16
  • 99
  • 167
132
votes
3 answers

Run a callback only if an attribute has changed in Rails

I have the following association in my app: # Page belongs_to :status I want to run a callback anytime the status_id of a page has changed. So, if page.status_id goes from 4 to 5, I want to be able to catch that. How to do so?
Hommer Smith
  • 26,772
  • 56
  • 167
  • 296
126
votes
5 answers

How to perform Callbacks in Objective-C

How to perform call back functions in Objective-C? I would just like to see some completed examples and I should understand it.
ReachConnection
  • 1,831
  • 6
  • 22
  • 32
125
votes
8 answers

How do I pass an extra parameter to the callback function in Javascript .filter() method?

I want to compare each string in an Array with a given string. My current implementation is: function startsWith(element) { return element.indexOf(wordToCompare) === 0; } addressBook.filter(startsWith); This simple function works, but only…
agente_secreto
  • 7,959
  • 16
  • 57
  • 83