我有几个函数可以对社交媒体API进行多次删除调用,我需要知道它们何时完成。我使用Parse Cloud Code进行这些调用,我不确定他们是否使用AJAX。我尝试了如下所述的ajaxStop方法:Waiting on multiple asynchronous calls to complete before continuing 但没有出现。我做了一个简单的
$(document).ajaxStart(function(){
console.log("ajax calls started");
});
但我的控制台中没有任何内容出现。
$('#delete').click(function(){
$(function(){
$(document).ajaxStop(function(){
$(this).unbind("ajaxStop");
console.log("All calls finished");
});
deleteTwitter();
deleteTumblr();
});
});
我不知道怎么回事,NodeJS也不是一个选择。我非常欣赏对此的一些见解,无论是关于Parse Cloud Code的知识还是我对AJAX如何工作的误解。谢谢。
答案 0 :(得分:2)
感谢@Fosco将我指向Promise。我每次拨打电话时都创建了一个自定义承诺,并在完成所有承诺时使用Parse.Promise.when()执行代码。
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");}
});
}
有关承诺的更详细说明,请参阅此处http://www.parse.com/docs/js/symbols/Parse.Promise.html。