I am working a page to help me sort my movie files. And instead of making one ajax request and waiting forever to see any progress, i am requesting the file info in chunks. I also apologize in advance, as this whole deferred/promise thing is really new to me, so I might not use all the correct terminology.
Here is how my code JavaScript looks
// Used to get info about a chunk of movies
var parse_movie_file = function (index){
return $.ajax({
type: 'GET',
url: '/movies/get_info',
dataType: "json",
data: {'movie_index': index}
});
};
$('form').submit(function(e){
// This first request gets a list of files to be parsed
$.ajax({
type: 'GET',
url: '/movies/get_list',
data: $(this).serialize(),
}).done(function(r){
// Create a Deferred object for parsing filenames
var parse_deferred = $.Deferred();
// Set deferred to resolve to start parsing filenames
parse_deferred.resolve();
for (var i = parse_max_progress - 1; i >= 0; i--) {
// Try/Throw/Catch keeps 0 from being used in all asynchronous parse_movie_file calls
try{throw i}
catch(ii){
parse_deferred = parse_deferred.then(function(){
return parse_movie_file(ii, table_body, row_template).done(function(r){
// Update page with response info
});
});
}
}
// <place holder>
});
});
This list of files can get pretty big and take a long time to go through all of them. I'm getting through large lists just fine, but there's times that I want to cancel the process before all deferred promises are resolved.
Can I break the promise chain and stop any open ajax requests?
Also, if I'm allowed a side question, if I move parse_deferred.resolve();
to // <place holder>
, I get an error saying that parse_deferred doesnt have a function resolve(). Any thought on that