Last active
August 29, 2015 14:18
-
-
Save tomhalley/72b52d6ae73d9155a282 to your computer and use it in GitHub Desktop.
jQuery Sequential Deferred
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var persistUploads = function () { | |
var deferred = $.Deferred(), | |
persistQueue = []; | |
for (var type in Behaviour.Uploads.files) { | |
Behaviour.Uploads.files[type].sort(function(a,b) { return a.order - b.order; }); | |
var image; | |
for (var a = 0; a < Behaviour.Uploads.files[type].length; a++) { | |
image = Behaviour.Uploads.files[type][a]; | |
if (image.status === STATUS_PERSIST) { | |
persistQueue.push(Behaviour.Uploads.insertUpload(image)); | |
} | |
} | |
for (var b = 0; b < Behaviour.Uploads.files[type].length; b++) { | |
image = Behaviour.Uploads.files[type][b]; | |
if (image.status === STATUS_UPDATE) { | |
persistQueue.push(Behaviour.Uploads.updateUpload(image)); | |
} | |
} | |
for (var c = 0; c < Behaviour.Uploads.files[type].length; c++) { | |
image = Behaviour.Uploads.files[type][c]; | |
if (image.status === STATUS_DELETE) { | |
persistQueue.push(Behaviour.Uploads.deleteUpload(image)); | |
} | |
} | |
} | |
$.when.apply($, persistQueue).then(function() { | |
deferred.resolve(); | |
}); | |
return deferred.promise(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment