EzDevInfo.com

callback interview questions

Top callback frequently asked interview questions

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.


Source: (StackOverflow)

Advertisements

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?


Source: (StackOverflow)

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 sometimes overkill is the only way to do things.


Source: (StackOverflow)

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);
  }
}

function postinsql(topicId)
{
  //alert(topicId);
}

I get a error that topicId is not defined Everything was working before i used the setTimeout() function.

I want my postinsql(topicId) function to be called after some time. What should i do?


Source: (StackOverflow)

How to remove all callback 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 onStop event, I need to remove them before finish the activity (somehow I called finish(), but it still call again and again). Is there anyway to remove all callbacks from a Handler?

Thanks.


Source: (StackOverflow)

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 above, this refers to the window.


Source: (StackOverflow)

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() {
    var myDiv = $("#my-div");
    // ERROR: Says data not defined
    $.post("someurl.php",someData,doSomething(data, myDiv),"json"); 
    // ERROR: Would pass in myDiv as curData (wrong)
    $.post("someurl.php",someData,doSomething(data, myDiv),"json"); 
}

function doSomething(curData, curDiv) {

}

I want to be able to pass in my own parameters to a callback, as well as the result returned from the AJAX call.

Thanks!


Source: (StackOverflow)

Rails: update_attribute vs update_attributes

Object.update_attribute(:only_one_field, "Some Value")
Object.update_attributes(:field1 => "value", :field2 => "value2", :field3 => "value3")

Both of these will update an object without having to explicitly tell AR to update.

Rails API says:

for update_attribute

Updates a single attribute and saves the record without going through the normal validation procedure. This is especially useful for boolean flags on existing records. The regular update_attribute method in Base is replaced with this when the validations module is mixed in, which it is by default.

for update_attributes

Updates all the attributes from the passed-in Hash and saves the record. If the object is invalid, the saving will fail and false will be returned.

So if I don't want to have the object validated I should use update_attribute. What if I have this update on a before_save, will it stackoverflow?

My question is does update_attribute also bypass the before save or just the validation.

Also, what is the correct syntax to pass a hash to update_attributes... check out my example at the top.


Source: (StackOverflow)

How to access the correct `this` / context 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 = {
    on: function(event, callback) {
        setTimeout(callback, 1000);
    }
};

// called as
var obj = new MyConstructor('foo', transport);

However, I'm not able to access the data property of the created object inside the callback. It looks like this does not refer to the object that was created but to an other one.

I also tried to use an object method instead of an anonymous function:

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', this.alert);
}

MyConstructor.prototype.alert = function() {
    alert(this.name);
};

but it exhibits the same problems.

How can I access the correct object?


Source: (StackOverflow)

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){
...
request(function(){
    // change happened
});

3. Node style callback ("nodeback"):

function getStuff(dat,callback){
...
getStuff("dataParam",function(err,data){

}

4. A whole library with node style callbacks:

API;
API.one(function(err,data){
    API.two(function(err,data2){
        API.three(function(err,data3){

        })
    });
});

How do I work with the API in promises, how do I "promisify" it?


Source: (StackOverflow)

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?

function tryMe (param1, param2) {
    alert (param1 + " and " + param2);
}

function callbackTester (callback, param1, param2) {
    callback (param1, param2);
}

callbackTester (tryMe, "hello", "goodbye");

Source: (StackOverflow)

How to return value from an asynchronous callback function? [duplicate]

This question already has an answer here:

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': address}, function(results, status) {
          results[0].geometry.location; // I want to return this value
      })

    }
    foo(); //result should be results[0].geometry.location; value

If I try to return that value just getting "undefined". I followed some ideas from SO, but still fails.

Those are:

function foo(address){
    var returnvalue;    
    geocoder.geocode( { 'address': address}, function(results, status) {
        returnvalue = results[0].geometry.location; 
    })
    return returnvalue; 
}
foo(); //still undefined

Source: (StackOverflow)

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 : function( param1, param2, callback ) {
    	// Do something with param1 and param2.
    	if ( arguments.length == 3 ) {
    		// Execute callback function.
    		// What is the "best" way to do this?
    	}
    },
    mySecondFunction : function() {
    	myFirstFunction( false, true, function() {
    		// When this anonymous function is called, execute it.
    	});
    }
};

In myFirstFunction, if I do return new callback(), then it works and executes the anonymous function, but that doesn't seem like the correct approach to me.


Source: (StackOverflow)

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. There is one sticking point for me that I can't seem to find any information about and it's not necessary to have seen the presentation to be able to answer this question. In this diagram ( http://i.imgur.com/GlYQF.gif ) on the return path there are various different callbacks to other methods. What I don't understand is how I declare what these methods are. In other words I understand the idea of a callback (a piece of code that gets called after a certain event has happened), but I don't know how to implement it and I haven't been able to find a suitable explanation for android online yet. The only way I've implemented callbacks so far have been overriding various methods (onActivityResult for example).

I feel like I have a basic understanding of the design pattern, but I keep on getting tripped up on how to handle the return path. Thank you for any help.


Source: (StackOverflow)