Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript callback - how to return the result?

I am struggling to totally understand callbacks and i am stumbling at the final hurdle.

Within JS I am calling a function which then calls a PHP function using a dojo rpc Json Service. I have stepped through the function in firebug and the PHP is executing and returning me the correct response via the callback but I don’t know how to return the value to the initial JS variable that invoked the JS function? E.g.

JS Function 1

Function one(){

Var test = getPhp(number);

}

function getPhp(number)
{

this.serviceBroker = new dojo.rpc.JsonService(baseUrl + '/index/json-rpc/');

    var result = serviceBroker.phpFunc(number);

    result.addCallback(
        function (response)
        {
            if (response.result == 'success')
            {
                return response.description;
               //I am trying to pass this value back to the 
               //var test value in   function one

            }
        }
    );
}

Basically i now need to pass response.description back to my var test variable in function one.

Any help is appreciated

like image 842
Pjn2020 Avatar asked Jun 23 '11 11:06

Pjn2020


People also ask

How do you return a value from a callback function?

There's no place for returned values to go. A callback function can return a value, in other words, but the code that calls the function won't pay attention to the return value. Yes, it makes fun sense to try and return a value from a promise callback. Unfortunately, it is not possible.

Does callback function have return statement?

No, callback should not be used with return.

How do you return a value from a JavaScript function?

JavaScript passes a value from a function back to the code that called it by using the return statement . The value to be returned is specified in the return keyword.

Which callback function is passed the returned data?

The function to which the callback is passed is often referred to as a higher-order function. Conversely, Higher-Order Functions operate on other functions by either taking them as arguments or by returning them.


1 Answers

This is not possible, since the callback is run asynchronously. This means that the getPhp function returns before the callback is executed (this is the definition of a callback, and one of the reasons asynchronous programming is hard ;-) ).

What you want to do is create a new method that uses the test variable. You need to call this method when the callback is executed.

i.e.

function one(result) {
  var test = result;
  // Do anything you like
}

function getPhp(number, callback) {
  this.serviceBroker = new dojo.rpc.JsonService(baseUrl + '/index/json-rpc/');
  result.addCallback(
    function (response)
    {
        if (response.result == 'success')
        {
           callback(response.description);
        }
    }
  );
}

getPhp(number, function(result) { one(result); });

This last method creates an 'anonymous function' that is passed to the getPhp function. This function gets executed at the time the response arrives. This way you can pass data to the one(number) function after the data arrives.

like image 113
Kamiel Wanrooij Avatar answered Oct 19 '22 21:10

Kamiel Wanrooij