Jon
Jon

Reputation: 8511

Perform asynchronous call before calling function

I have a function, authenticate() that uses $post() to retrieve a session key from my server. I believe $post is an asynchronous call. Whenever I perform any action in my script, I want to ensure that I have a sessionKey, and if I do not have a sessionKey I want to authenticate(). My problem is how do I run performTask() after I have called authenicate()?

function foo() {
    if (sessionKey) {
        performTask();
    } else {
        authenciate();
        performTask();
    }
}

function authenticate() {       
    $.post(url, function(data) {
        sessionKey = data.sessionKey;
    });
}

EDIT: I also do not want to put in authenticate()'s callback function performTask() as authenticate() will be called from several different functions.

Upvotes: 1

Views: 160

Answers (4)

Meligy
Meligy

Reputation: 36594

Check this question: How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

It has an example of doing AJAX synchronously. Basically you pass an async:false param, and might require using the ajax() method instead of the post() shortcut to it.

Update:

Another thing you can do is wrap the whole thing in a function like:

function foo() {
    authenciate(function() {
       performTask();
    });
}

function authenticate(callback) {     

    if (!callback) { return; }

    if (sessionKey) {
        callback();
    } else {
        $.post(url, function(data) {
            sessionKey = data.sessionKey;
            callback();
        });
    }
}

Upvotes: 0

CodeThug
CodeThug

Reputation: 3192

You can pass in a callback to the authenticate method. If authenticate is called from 20 different places, you can pass in 20 different callback methods.

function foo() {
    if (sessionKey) {
        performTask();
    } else {
        authenciate(function() {
           performTask();
        });
    }
}

function authenticate(callback) {       
    $.post(url, function(data) {
        sessionKey = data.sessionKey;
        if (callback) callback();
    });
}

Upvotes: 1

Ry-
Ry-

Reputation: 224906

Pass the callback to authenticate() itself using jQuery's Promise interface (see documentation):

function authenticate(callback) {
    var rq = $.post(url, function(data) {
        sessionKey = data.sessionKey;
    });

    if(callback) {
        rq.success(callback);
    }
}

Now just call authenticate(performTask), for example.

Upvotes: 2

yoozer8
yoozer8

Reputation: 7489

JQuery's AJAX calls have callback functions (success and error). You can pass in the after-authentication action as the callback to do it after authentication has completed.

Alternately, you can set am option to make the calls synchronous.

Sorry for not linking to the documentation; I'm posting from my phone. Just google "jquery.ajax" to find the documentation.

*EDIT:*As a response to your edit, you could give your authenticate method a parameter to use as a callback function if you want to change what happens after the call (or do nothing) on a case-by- case basis.

Upvotes: 0

Related Questions