1

I have a couple functions that make multiple delete calls to social media APIs, and I need to know when all of them are finished. I'm using Parse Cloud Code to make these calls, and I'm not sure if they use AJAX. I tried the ajaxStop method as described here: Waiting on multiple asynchronous calls to complete before continuing but nothing came up. I did a simple

$(document).ajaxStart(function(){
   console.log("ajax calls started");
});

but nothing came up in my console for that either.

$('#delete').click(function(){

    $(function(){
        $(document).ajaxStop(function(){
            $(this).unbind("ajaxStop");
            console.log("All calls finished");          
        });
        deleteTwitter();
        deleteTumblr();
    });

});

I'm not sure how else to go about this, also, NodeJS isn't an option. I'd really appreciate some insight on this, whether it's knowledge about Parse Cloud Code or just something I'm misunderstanding about how AJAX works. Thank you.

Community
  • 1
  • 1
Henry
  • 291
  • 2
  • 7
  • 14
  • I don't see any Parse related code here.. – Fosco May 31 '14 at 00:17
  • FYI, `ajaxStart()` and `ajaxStop()` will only work for ajax calls made using jQuery's ajax functions. – jfriend00 May 31 '14 at 01:20
  • @Fosco the deleteTwitter() function just calls a Parse.Cloud.run() which makes an HTTP request to the Twitter API (same with tumblr) – Henry May 31 '14 at 06:11
  • look into the jquery [when()](http://api.jquery.com/jQuery.when/) it is made to tell you when a number of async calls are complete – Patrick Gunderson May 31 '14 at 06:47
  • 2
    Parse.Cloud.run returns a promise. You can create an array with the return values from multiple calls to run, and then use Parse.Promise.when(arrayofpromises).then(function() { ... }); to run code when they complete. Let me know if you want me to post a full example. – Fosco May 31 '14 at 06:48
  • @Patrick Gunderson I tried the when() thing before but it executed before the Parse code finished – Henry May 31 '14 at 17:44
  • @Fosco I would really appreciate if you could post a full example, that sounds like it would be super helpful. – Henry May 31 '14 at 17:44

1 Answers1

2

Thanks to @Fosco for pointing me towards Promises. I just created a custom promise each time I made a call, and used Parse.Promise.when() to execute code when all promises are fulfilled.

var promiseArray = [];
deleteSomething();

Parse.Promise.when(promiseArray).then(function(){
    //triggers when all promises fulfilled
    console.log("All delete calls completed");
});

//------------------------------------------------
function deleteSomething()
{
    var promise = new Parse.Promise;
    promiseArray.push(promise);

    Parse.Cloud.run('foo', {...}, {
        success(): function(){ promise.resolve("Call finished");},
        error():{promise.reject("Call failed");}
    });
}

See here http://www.parse.com/docs/js/symbols/Parse.Promise.html for a more detailed explanation of promises.

Henry
  • 291
  • 2
  • 7
  • 14