我有一个数组来上传数据,我希望它们逐个发送到服务器。 我希望在发送下一个之前完成前一个。 实际上,在每次回复之后,我想决定是否发送下一个。
while (packetCount < bulkUploadPackets.length) {
d = d.then(save(bulkUploadPackets[packetCount]))
.then(function(uploadResponse) {
//I want to come here after first call complete
//before second call is fired and so on
packetCount++;
});
}
save: function(modelToSave) {
var defer = $.Deferred();
var self = this;
this.model = modelToSave;
Backbone.sync('create', this, {
success: function(data, textStatus, jqXHR) {
console.log("success" + data);
defer.resolve(data);
},
error: function(response) {
defer.reject(errorObj);
}
});
return defer.promise();
}
答案 0 :(得分:1)
你可以使用递归函数并在其中放入递归调用:
(function loop() {
if (packetCount<bulkUploadPackets.length) {
d = d.then(save(bulkUploadPackets[packetCount]))
.then(function(uploadResponse){
//I want to come here after first call complete before second call is fired and so on
packetCount++;
loop();
});
}
})();