1

I want to return true or false after making this ajax call:

function write_csv(data, path, file) {

    $.ajax({
        url: 'functions.php',
        type: 'POST',
        data: {
            operation: 'SAVE_CSV',
            save_path: path,
            save_file: file,
            save_string: data
        },
        success: function(result) {
            console.log('write_file(' + path + file + '); -done');

            return true; /* <-- */

        }
    });
}

Example use case of what I want:

function make_csv () {

    /* 
    |
    V
    */

    if (write_csv(my_data, my_path, 'export.csv') == true) {
        go_on();
    }

    function go_on() {
        alert('YEAH!');
    }

}

I know it's async, but maybe someone has another idea. I won't do it with if's and stuff...

mhatch
  • 4,441
  • 6
  • 36
  • 62
Tarek Haddad
  • 55
  • 1
  • 4
  • 3
    possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Sebastian Simon Sep 09 '15 at 17:03

2 Answers2

3

You can use Promises or callbacks to accomplish what you want.

function write_csv(data, path, file, callback) {

    $.ajax({
        url: 'functions.php',
        type: 'POST',
        data: {
            operation: 'SAVE_CSV',
            save_path: path,
            save_file: file,
            save_string: data
        },
        success: function(result) {
            console.log('write_file(' + path + file + '); -done');

            callback(true); /* <-- */

        }
    });
}

And:

function make_csv () {

    /* 
    |
    V
    */

    function go_on() {
        alert('YEAH!');
    }

    write_csv(my_data, my_path, 'export.csv', function(result) {
        if (result == true) {
            go_on();
        }
    });
}
Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89
0

I am going to go off of jQuery convention and give you a "jQuery" answer since that's what you are using.

In jQuery, you have the ability to pass in a callback (a function to "call" once the actual function you are using finishes) in most jQuery methods. jQuery's convention is to have the callback as the last parameter you pass in into a function. In your example, your write_csv() function would look like this with an extra callback as the last parameter:

function write_csv(data, path, file, callback){
    $.ajax({
        url: 'functions.php',
        type: 'POST',
        data: {
            operation: 'SAVE_CSV',
            save_path: path,
            save_file: file,
            save_string: data
        },
        success: function(result) {
            console.log('write_file(' + path + file + '); -done');
            callback(true);
        }
        error: function(result){
            console.log('async failed');
            callback(false);
        } 
    });
}

Notice the error key being passed in and the changes made to the success key in the $.ajax() function.

Now, when you want to use your async function in an if conditional statement, you can use

write_csv(my_data, my_path, 'export.csv', function(response){
    if(response === true){
        go_on()
    }
    else if(response === false){
        // do something else
    }
});