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
309
votes
11 answers

What is the purpose of willSet and didSet in Swift?

Swift has a property declaration syntax very similar to C#'s: var foo: Int { get { return getFoo() } set { setFoo(newValue) } } However, it also has willSet and didSet actions. These are called before and after the setter is called,…
zneak
  • 134,922
  • 42
  • 253
  • 328
297
votes
17 answers

Callback after all asynchronous forEach callbacks are completed

As the title suggests. How do I do this? I want to call whenAllDone() after the forEach-loop has gone through each element and done some asynchronous processing. [1, 2, 3].forEach( function(item, index, array, done) { asyncFunction(item,…
Dan Andreasson
  • 15,380
  • 5
  • 29
  • 30
293
votes
7 answers

When to use React setState callback

When a react component state changes, the render method is called. Hence for any state change, an action can be performed in the render methods body. Is there a particular use case for the setState callback then?
Sahil Jain
  • 3,649
  • 2
  • 14
  • 16
270
votes
11 answers

Rails: #update_attribute vs #update_attributes

obj.update_attribute(:only_one_field, 'Some Value') obj.update_attributes(field1: 'value', field2: 'value2', field3: 'value3') Both of these will update an object without having to explicitly tell ActiveRecord to update. Rails API…
thenengah
  • 42,557
  • 33
  • 113
  • 157
261
votes
14 answers

jQuery pass more parameters into callback

Is there a way to pass more data into a callback function in jQuery? I have two functions and I want the callback to the $.post, for example, to pass in both the resulting data of the AJAX call, as well as a few custom arguments function clicked()…
atp
  • 30,132
  • 47
  • 125
  • 187
260
votes
11 answers

Defining TypeScript callback type

I've got the following class in TypeScript: class CallbackTest { public myCallback; public doWork(): void { //doing some work... this.myCallback(); //calling callback } } I am using the class like this: var test =…
nikeee
  • 10,248
  • 7
  • 40
  • 66
254
votes
9 answers

How to remove all callbacks from a Handler?

I have a Handler from my sub-Activity that was called by the main Activity. This Handler is used by sub-classes to postDelay some Runnables, and I can't manage them. Now, in the onStop event, I need to remove them before finishing the Activity…
Luke Vo
  • 17,859
  • 21
  • 105
  • 181
216
votes
10 answers

What is a callback?

What's a callback and how is it implemented in C#?
cam
  • 8,725
  • 18
  • 57
  • 81
207
votes
7 answers

Determine what attributes were changed in Rails after_save callback?

I'm setting up an after_save callback in my model observer to send a notification only if the model's published attribute was changed from false to true. Since methods such as changed? are only useful before the model is saved, the way I'm currently…
modulaaron
  • 2,856
  • 3
  • 24
  • 28
206
votes
18 answers

Callback functions in Java

Is there a way to pass a call back function in a Java method? The behavior I'm trying to mimic is a .Net Delegate being passed to a function. I've seen people suggesting creating a separate object but that seems overkill, however I am aware that…
Omar Kooheji
  • 54,530
  • 68
  • 182
  • 238
193
votes
12 answers

How can I run a JavaScript callback when an image is loaded?

I want to know when an image has finished loading. Is there a way to do it with a callback? If not, is there a way to do it at all?
matt
190
votes
12 answers

Java executors: how to be notified, without blocking, when a task completes?

Say I have a queue full of tasks which I need to submit to an executor service. I want them processed one at a time. The simplest way I can think of is to: Take a task from the queue Submit it to the executor Call .get on the returned Future and…
Shahbaz
  • 10,395
  • 21
  • 54
  • 83
190
votes
7 answers

Should I use std::function or a function pointer in C++?

When implementing a callback function in C++, should I still use the C-style function pointer: void (*callbackFunc)(int); Or should I make use of std::function: std::function< void(int) > callbackFunc;
Jan Swart
  • 6,761
  • 10
  • 36
  • 45
184
votes
18 answers

Callback to a Fragment from a DialogFragment

Question: How does one create a callback from a DialogFragment to another Fragment. In my case, the Activity involved should be completely unaware of the DialogFragment. Consider I have public class MyFragment extends Fragment implements…
eternalmatt
  • 3,524
  • 4
  • 25
  • 25
182
votes
10 answers

What is a "callback" in C and how are they implemented?

From the reading that I have done, Core Audio relies heavily on callbacks (and C++, but that's another story). I understand the concept (sort of) of setting up a function that is called by another function repeatedly to accomplish a task. I just…
noizetoys
  • 2,923
  • 5
  • 24
  • 15