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.
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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments