jQuery AJAX chained calls + Celery in Django

Posted by user1029968 on Stack Overflow See other posts from Stack Overflow or by user1029968
Published on 2012-11-02T21:53:48Z Indexed on 2012/11/02 23:01 UTC
Read the original article Hit count: 259

Filed under:
|
|
|

Currently clicking one of the links in my application, triggers AJAX call (GET) that - if succeeds - triggers the second one and this second one - if succeeds - calls the third one.

This way user can be informed which part of process started when clicking the link is currently ongoing.

So in the template file in Django project, click callback body for link mentioned looks like below:

$("#the-link").click(function(item)) {
    // CALL 1
    $.ajax({
        url: {% url ajax_call_1 %},
        data: {
            // something
        }
    })
    .done(function(call1Result) {

        // CALL 2
        $.ajax({
            url: {% url ajax_call_1 %},
            data: {
                // call1Result passed here to CALL 2
            }
        })
        .done(function(call2Result) {

            // CALL 3
            $.ajax({
                url: {%url ajax_call_3 %},
                data: {
                    // call2Result passed here to CALL 3
                }
            })
            .done(function(call3Result) { // expected result if everything went fine
                console.log("wow, it worked!");
                console.log(call3Result);
            })
            .fail(function(errorObject) {
                console.log("call3 failed");
                console.log(errorObject);
            }
        })
        .fail(function(errorObject)) {
            console.log("call2 failed");
            console.log(errorObject);
        }
    })
    .fail(function(errorObject) {
        console.log("call1 failed");
        console.log(errorObject);
    });
});

This works fine for me. The thing is, I'd like to prevent interrupting the following calls if the user closes the browser and the calls are not finished (as it will take some time to finish all three), as there is some additional logic in Django view functions called in each GET request.

For example, if user clicks the link and closes the browser during CALL 1, is it possible to somehow go on with the following CALL 2 and CALL 3?

I know that normally I'd be able to use Celery Task to process the function but is it still possible here with the chained calls mentioned?

Any help is much appreciated!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about AJAX