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
1881
votes
15 answers

How to access the correct `this` inside a callback

I have a constructor function which registers an event handler: function MyConstructor(data, transport) { this.data = data; transport.on('data', function () { alert(this.data); }); } // Mock transport object var transport = { …
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1007
votes
30 answers

How can I pass a parameter to a setTimeout() callback?

I have some JavaScript code that looks like: function statechangedPostQuestion() { //alert("statechangedPostQuestion"); if (xmlhttp.readyState==4) { var topicId = xmlhttp.responseText; setTimeout("postinsql(topicId)",4000); …
Zeeshan Rang
  • 19,375
  • 28
  • 72
  • 100
860
votes
24 answers

How do I convert an existing callback API to promises?

I want to work with promises but I have a callback API in a format like: 1. DOM load or other one time event: window.onload; // set to callback ... window.onload = function() { }; 2. Plain callback: function request(onChangeHandler) { …
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
827
votes
22 answers

What is a callback function?

What is a callback function?
paul
772
votes
20 answers

How do I create delegates in Objective-C?

I know how delegates work, and I know how I can use them. But how do I create them?
Andy Jacobs
  • 15,187
  • 13
  • 60
  • 91
572
votes
3 answers

How to return value from an asynchronous callback function?

This question is asked many times in SO. But still I can't get stuff. I want to get some value from callback. Look at the script below for clarification. function foo(address){ // google map stuff geocoder.geocode( { 'address':…
Gowri
  • 16,587
  • 26
  • 100
  • 160
507
votes
11 answers

Aren't promises just callbacks?

I've been developing JavaScript for a few years and I don't understand the fuss about promises at all. It seems like all I do is change: api(function(result){ api2(function(result2){ api3(function(result3){ // do work …
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
427
votes
12 answers

Callback functions in C++

In C++, when and how do you use a callback function? EDIT: I would like to see a simple example to write a callback function.
cpx
  • 17,009
  • 20
  • 87
  • 142
416
votes
5 answers

Difference between array_map, array_walk and array_filter

What exactly is the difference between array_map, array_walk and array_filter. What I could see from documentation is that you could pass a callback function to perform an action on the supplied array. But I don't seem to find any particular…
Web Logic
405
votes
34 answers

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?
Yahoo-Me
  • 4,933
  • 5
  • 27
  • 26
363
votes
17 answers

JavaScript: Passing parameters to a callback function

I'm trying to pass some parameter to a function used as callback, how can I do that? This is my try: function tryMe(param1, param2) { alert(param1 + " and " + param2); } function callbackTester(callback, param1, param2) { callback(param1,…
vitto
  • 19,094
  • 31
  • 91
  • 130
343
votes
11 answers

Create a custom callback in JavaScript

All I need to do is to execute a callback function when my current function execution ends. function LoadData() { alert('The data has been loaded'); //Call my callback with parameters. For example, //callback(loadedData ,…
Amgad Fahmi
  • 4,349
  • 3
  • 19
  • 18
320
votes
11 answers

How to make a function wait until a callback has been called using node.js

I have a simplified function that looks like this: function(query) { myApi.exec('SomeCommand', function(response) { return response; }); } Basically i want it to call myApi.exec, and return the response that is given in the callback lambda.…
Chris
  • 39,719
  • 45
  • 189
  • 235
319
votes
10 answers

Subscribe is deprecated: Use an observer instead of an error callback

When I run the linter it says: subscribe is deprecated: Use an observer instead of an error callback Code from this angular app: this.userService.updateUser(data).pipe( tap(() => {bla bla bla}) ).subscribe( …
ismaestro
  • 7,561
  • 8
  • 37
  • 50
315
votes
6 answers

Pass correct "this" context to setTimeout callback?

How do I pass context into setTimeout? I want to call this.tip.destroy() if this.options.destroyOnHide after 1000 ms. How can I do that? if (this.options.destroyOnHide) { setTimeout(function() { this.tip.destroy() }, 1000); } When I try the…
JamesBrownIsDead
  • 4,655
  • 6
  • 30
  • 29
1
2 3
99 100