0

With async:set to false, the function is returning the correct value. However it blocks the browser. I've tried to add callbacks but it not returning the correct value!

function find_route(array_bustops){
        var myresults;
            $.ajax({

            type: 'POST',
                async: true,  //with async:false it works
            url: 'find_routenum.php',

            data:{

                array_bustops:JSON.stringify(array_bustops)

            },
            dataType:'json', //html,xml

            success: function(my_results){

                myresults=my_results;


            },

            error:function(x,e){
                if(x.status==0){
                alert('You are offline!!\n Please Check Your Network.');
                }else if(x.status==404){
                alert('Requested URL not found.');

            }


        });

        return myresults;
    }

WITH CALLBACK:

function find_route(array_bustops,callback){
        var myresults;
            $.ajax({

            type: 'POST',
                async: true,  //with async:false it works
            url: 'find_routenum.php',

            data:{

                array_bustops:JSON.stringify(array_bustops)

            },
            dataType:'json', //html,xml

            success: function(my_results){

                callback(array_bustops,my_results)


            },

            error:function(x,e){
                if(x.status==0){
                alert('You are offline!!\n Please Check Your Network.');
                }else if(x.status==404){
                alert('Requested URL not found.');

            }


        });

        return myresults;
    }
    function find_route2(myresults){
           return myresults;
    }

And then i call the function as follows:

arr_one=find_route(arr,find_route2)

but arr_one returns undefined.

EDIT: it still is not working with async:set to true

6
  • Do you see any javascript errors? Commented Mar 8, 2012 at 14:09
  • Does "Synchronous Ajax Request" cause some slow down in code execution? Commented Mar 8, 2012 at 14:17
  • jens, what happens is that the main executing thread gets locked, js is not able to do anything else while it's waiting for the ajax request to come back. Commented Mar 8, 2012 at 14:23
  • i've changed to code to function find_bus_route2(array_bustops,my_results){ return my_results; } but arr_one is still undefined! Commented Mar 8, 2012 at 14:28
  • 1
    Ok I'll try to get something together give me a few min. Commented Mar 8, 2012 at 15:46

3 Answers 3

3

arr_one=find_route(arr,find_route2)

this got nothing to do with async but with the architecture of your code.

you declare myresults in the scope of find_route, when you call find_route the function return the value of myresults who is undefined because you delcare it only.

Try to declare var myresults='somevalue'; in your function

Now your function return 'somevalue'

This happen because the call to ajax method will not stop the function execution and the success method will be executed way after find_route is called and returned just put a console.log in there you'll see that it will get log eventually when the success is called.

It work when async false is set because async false as you discover stop the function execution from returning before the ajax call is back

Edit: possible solution

function DataListener(val){
    this.val = val;
    this.remoteFetch();
}

DataListener.prototype.set = function(val){
    this.val = val;
    this.notifyListener(val)
}
DataListener.prototype.get = function(val){
    return this.val;
}

DataListener.prototype.remoteFetch = function(){
    $.ajax({
        success:$.proxy(this, 'set')
    })
}

var arr_one = new DataListener('somevalue');
alert(arr_one.get());// alert somevalue

function doSomething(arr_one_value){
    alert(arr_one_value); // the new value set from the ajax call   
}

arr_one.addListener(doSomething);
Sign up to request clarification or add additional context in comments.

3 Comments

You're right but then instead of returning undefined. arr_one will have as value "somevalue". please have a look at my code jsfiddle.net/YtkCh
hmm the callback function find_route2 is getting the correct value but how do i return the value to arr_one? I think the problem comes here arr_one=find_route(arr,find_route2) find_route 2 is returning the correct value. but how do i assign that value to arr_one?
Any idea what i should do? i'm really stuck here!
1

At a glance, you are passing two arguments to your callback, but in your actual callback function you only accept one.

callback(array_bustops,my_results); 

// your callback function  
function find_route2(myresults){
       return myresults;
}

Try passing just the results to the argument.

callback(my_results);

6 Comments

oops sorry i made the modification but ran the code with async set to false; I'm totally lost, it still not working
jens, elmuchacho has the correct answer. You will need to restructure your code. JS will not wait for the response from the ajax request, you will have to put the code you want executed after the ajax into the callback function.
here is my code jsfiddle.net/YtkCh. alert find1 is returning undefined. alert find2 is returning some value
jens the fiddle is incomplete ?
jens, what it really comes down to is, what do you want to do with the response ?
|
0
function find_route(array_bustops){
            $.ajax({

            type: 'POST',
                async: true,  //with async:false it works
            url: 'find_routenum.php',

            data:{

                array_bustops:JSON.stringify(array_bustops)

            },
            dataType:'json', //html,xml

            success: function(my_results){

                // HOW ABOUT PUTTING THE NEXT DESIRED FUNCTIONALITY HERE? //
                return my_results 

            },

            error:function(x,e) {
                if(x.status==0) {
                    alert('You are offline!!\n Please Check Your Network.');
                }else if(x.status==404) {
                    alert('Requested URL not found.');
                }
                //AND THE DUMMY RETURN VALUE OF THE FUNCTION IS HERE//
                return FALSE;
            }


        });

        return myresults;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.