1

According to jQuery docs:

To execute a function after two ajax requests are successful:

$.when($.ajax("/page1.php"), $.ajax("/page2.php")).done(function(a1,  a2){
/* a1 and a2 are arguments resolved for the 
    page1 and page2 ajax requests, respectively */
   var jqXHR = a1[2]; /* arguments are [ "success", statusText, jqXHR ] */
  if ( /Whip It/.test(jqXHR.responseText) ) {
  alert("First page has 'Whip It' somewhere.");
}
});

I have this:

    $.when(
        file2store('resources/states_obj.txt?v=2', 'states'),
        file2store('resources/lgas_obj.txt?v=2', 'lgas'),
        file2store('resources/villages_obj.txt?v=2', 'villages'),
        file2store('resources/staff_obj.txt?v=2', 'staff')
    ).done(function(){
            console.log('All data put');
    }); 


    function file2store(url, store_name){
        $.getJSON(url, function(obj){ 

            db.put(store_name, obj).then(
              function(ids) {
                console.log(store_name+': '+ids.length + ' items put.');
              }, function(e) {
                throw e;
              }
            );

        });
    }

The variable db in the ajax callback is a global variable for the indexedDB Storage object, obtained (not shown here) at the top of the script.

  1. Is this a correct usage of the jQuery Deferred construct?

  2. Will the function calls file2store be queued, meaning, make sure that one call finishes before the next one kicks in?

Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
Ifedi Okonkwo
  • 3,406
  • 4
  • 33
  • 45
  • A function without an explicit return statement returns `undefined` and, as written, that's what `file2store` will do. `jQuery.when()` accepts promises as its arguments, so for your code to work, `file2store` must return a promise - ie `return db.put(...).then(...);` – Roamer-1888 Jun 29 '14 at 18:26
  • Side note ... hope you are aware that indexedDB.open is also asynchronous, and your 'db' variable is not guaranteed to be defined and open at the point in time that you are accessing it. – Josh Jun 30 '14 at 03:23

1 Answers1

1

is this a correct usage of the jQuery Deferred construct?

Not quite. The when() method accepts the deferred object, as returned from a jQuery method which creates an AJAX request. As such, you need to return that object for your code to work:

function file2store(url, store_name){
    return $.getJSON(url, function(obj) { // note the return here.
        // rest of your code... 
    });
}

Will the function calls file2store be queued, meaning, make sure that one call finishes before the next one kicks in?

No. They are created in the order you provide them however they will be completed in whatever order the server responds to them.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • I see. Then the returned jqXHR objects can be passed on as `.done(function(a, b, c, d){ //do stuff with a, b, c or d });`. So beside the point you made, how does the `db.put()` which is a NESTED ASYNCHRONOUS routine within the `$.ajax()` asynchronous routine affect things? E.g. Can data be returned from within it into the outer scope??? Guess not, Outch!!! – Ifedi Okonkwo Jun 29 '14 at 16:11
  • Following up: I guess the `db.put()` should indeed be extracted from that inner scope, and rather, we should do this: `.done(function(a, b, c, d){ //pass the respective response JSON obj to its own `db.put()` within this place });`. Right? – Ifedi Okonkwo Jun 29 '14 at 16:17
  • I am having to accept your answer because I reckon it is factually right, addressing the specific questions I had asked. Was also quite useful in guiding me towards an eventual solution. Must say, though, that I'd been expecting a follow-up input regarding my last couple of comments on your answer. Cheers! – Ifedi Okonkwo Jul 08 '14 at 17:09