-1

I am using jQuery promises to return the data set from a web SQL database table and using $.when, I can return the data set and use it inside $.when according to this link

Return multiple rows as an array from a WebSQL databse using javaScript

But how can I use that data set outside $.when?

Here is the source for query:

function getContacts(){
    var defer = $.Deferred();       
    db.transaction(function(tx) {
        tx.executeSql('SELECT * FROM contacts', [], function(tx, results) {
           defer.resolve(results.rows);
        },function(tx, err){
            alert("An internal error occurred. " + err.message);
        });
    });
    return defer.promise();
}

Here is the source for returning and using that value inside $.when:

var contact = getContacts();
$.when(contact).done(function(cont) {
    for(var i = 0; i < cont.length; i++){
        // can use the 'cont' variable to access data in the table 
        // inside this '$.when', but can't use it outside this $.when block
    }
});

How can I achieve this?

Community
  • 1
  • 1
vidulaJ
  • 1,232
  • 1
  • 16
  • 31
  • 1
    You can't really use it outside the `$.when().done()` callback because that's the only place you reliably know that it exists. Anywhere else the timing is completely unknown for when the value is ready. You can call any other function from within there and pass it the data if you want, but this is how you must work with async responses. – jfriend00 Oct 09 '14 at 06:46
  • I can't pass the data set to a variable inside $.when().done() and call it outside. Can I? – vidulaJ Oct 09 '14 at 07:00
  • Sorry, I don't understand what your last comment means. An important part of understanding promises and async coding is that the `.done()` handler is called sometime in the future and you don't really know when it will be called. So, you can deposit the result in some other outside variable from within the `.done()` handler, but your other code won't how to wait for the data to be there unless it is itself called from within the `.done()` handler. – jfriend00 Oct 09 '14 at 07:01
  • Can I do this if I use a variable to assign the data inside $.when().done() and use it outside? – vidulaJ Oct 09 '14 at 07:05
  • 2
    No - not reliably. It appears you just don't understand the issue of asynchronous timing. It might help to read [this answer](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) which is about an ajax call, but has the same async issue. – jfriend00 Oct 09 '14 at 07:06
  • Yeah, I got your point jfriend00. It is virtually impossible to pick the right time even with an infinite while loop though. (That was tried too though ;-)) – vidulaJ Oct 09 '14 at 09:24
  • Can someone at least explain why the down vote? – vidulaJ Oct 10 '14 at 03:33
  • What are you still expecting to get out of this question? We've told you that you can't reliably use the value outside of the completion handler unless you call a function from within the completion handler and pass the value to that function. That's how async functionality works in Javascript. There really is no more to say. – jfriend00 Oct 10 '14 at 03:46

1 Answers1

0

You should have modified your result, so that it will only return the data.

function getContacts(){
    var defer = $.Deferred();       
    db.transaction(function(tx) {
        tx.executeSql('SELECT * FROM contacts', [], function(tx, results) {
           var data = [];
           if (results.rows.length > 0) {
               for (var i = 0; i < results.rows.length; i++) {
                   data.push(results.rows.item(i));
               }
           }
           defer.resolve(data);
        },function(tx, err){
            alert("An internal error occurred. " + err.message);
        });
    });
    return defer.promise();
}

To access your data try this.

var fetchContacts = getContacts();
var contacts = [];
$.when(fetchContacts).then(function (cont) {
    contacts = cont;
}).done(function () {
    // contacts is now not empty
});

Refer to my sample http://jsfiddle.net/xalvin11/6htanxhs/

Alvin Magalona
  • 771
  • 3
  • 13