0

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

master_ruko
  • 629
  • 1
  • 4
  • 22
  • Possible duplicate of [How to cancel an EMCAScript6 (vanilla JavaScript) promise chain](https://stackoverflow.com/questions/29478751/how-to-cancel-an-emcascript6-vanilla-javascript-promise-chain) or [Abort Ajax requests using jQuery](https://stackoverflow.com/questions/446594/abort-ajax-requests-using-jquery) – Saeed Sep 11 '18 at 08:11
  • `Try/Throw/Catch keeps 0 from being used` - really? why not `for (var i = parse_max_progress - 1; i > 0; i--) {` instead? – Jaromanda X Sep 11 '18 at 08:39
  • @JaromandaX Yes, [really](https://stackoverflow.com/a/15035083/1048572). But this is absolutely hilarious, I would never have expected anyone to use this "feature" instead of [the standard IIFE solution for closures in loops](https://stackoverflow.com/q/750486/1048572). Or just `for (let i = …` today. – Bergi Sep 11 '18 at 12:48
  • "*I get an error saying that parse_deferred doesnt have a function resolve()*" - yes of course, because you've previously overwritten it with a promise: `parse_deferred = parse_deferred.then(…)`. – Bergi Sep 11 '18 at 12:50
  • @Bergi - I misunderstood the purpose of try/catch in that code ... but I see what it is now!! WOW! I thought something completely different (avoiding i == 0 but that makes no sense now that I think about it) – Jaromanda X Sep 11 '18 at 12:54
  • @Bergi The reason I used the try catch is because I am extremely novice to JavaScript. When I searched an answer to this problem, that was the solution I found. Thank you for pointing out a better one. – master_ruko Sep 11 '18 at 18:03
  • @master_ruko Where did you find this? This is a very exotic solution that will cause every experienced JS developer (like Jaromanda) to rub their eyes in amazement. (I might expect to see it generated by a transpiler, but not in written source code). – Bergi Sep 11 '18 at 18:13
  • @Bergi I honestly don't remember, It was like 5AM when I found it and I was practically falling asleep. I do recall reading that the try catch was what a transpiler would generate, but I really dont remember any more than that. I have switched it to using the IIFE solution now. My buddy is trying to tell me that I should have used recursion to accomplish this, any thoughts on that? – master_ruko Sep 11 '18 at 19:16
  • @master_ruko Yes, using a recursive approach will make it easier to implement the cancellation as well – Bergi Sep 11 '18 at 19:19

0 Answers0