1

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.

Jon
  • 8,205
  • 25
  • 87
  • 146

4 Answers4

2

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.

Ry-
  • 218,210
  • 55
  • 464
  • 476
1

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();
    });
}
josh3736
  • 139,160
  • 33
  • 216
  • 263
CodeThug
  • 3,054
  • 1
  • 21
  • 17
  • @icu222much: This answer is on the right track, but it's missing one important piece that @minitech's answer has -- checking that a callback was passed to `authenticate` before trying to invoke it. (`if (callback) callback();`) Otherwise, simply calling `authenticate();` (without a callback) will throw a `ReferenceError` . I've taken the liberty of adding the appropriate check. – josh3736 Feb 29 '12 at 03:58
0

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.

yoozer8
  • 7,361
  • 7
  • 58
  • 93
0

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();
        });
    }
}
Community
  • 1
  • 1
Meligy
  • 35,654
  • 11
  • 85
  • 109