0

I am writing the following function and I need to return myJsonString. When I execute the function it runs asynchronously. For making it synchronous I have used when(),then() but still it returns nothing.

Kindly suggest a way.

var myJsonString;
var items = [];

function getConfig() {
    $.when(offlinedb.configuration.toArray(function (documents) {
        $.each(documents, function (i, aaa) {
            var obj = {};
            var temp = aaa.Property;
            var tempObj = aaa.Value;
            obj[temp] = tempObj;
            items.push(obj);
        });
        myJsonString = JSON.stringify(items);
    })).then(function (y, yy) {
        console.log(myJsonString);
        // return does not work here..
    });
    return myJsonString;
} 

Edited my code:

var items = [];
var myJsonString;
function getConfig(){
    return offlinedb.configuration.toArray()
   .then(function(documents) {
             $.each(documents,function (i,aaa){
                var obj={};
                var temp=aaa.Property;
                var tempObj= aaa.Value;
                obj[temp]=tempObj;
                items.push(obj);
           });
          myJsonString = JSON.stringify(items);
          return myJsonString;
      });
    }
xTMNTxRaphaelx
  • 1,387
  • 3
  • 11
  • 14
  • You don't return things in AJAX you do stuff inside the callbacks. Attempting to make synchronous AJAX requests is not good advice, why use AJAX in the first place? Read here for more info http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – elclanrs Nov 13 '13 at 07:43
  • I am not using AJAX.I have mentioned I am using Javascript – xTMNTxRaphaelx Nov 13 '13 at 11:48
  • it should work, do you get any error message on the console ? also try to put console.log() statements into it to see what is going on – Gabor Dolla Nov 14 '13 at 09:14
  • Sir actually 'offline.configuration.toArray()' returns an entire entity set of database which cannot be stringify. Console works fine and no error messages – xTMNTxRaphaelx Nov 14 '13 at 10:24

2 Answers2

1

you can not turn async to sync. you will not be able to do things like:

var config = getConfig()

function getConfig() {
     // call async functions
     return something;
}

you'll need to use promises, like

getConfig()
.then(function(config) {
     // you can use config here
})

and in getConfig function you create a promise chain:

function getConfig() {
   return offlinedb.configuration.toArray()
   .then(function(documents) {
      // this part is sync
      // process your documents
      return result;
   })
}
Gabor Dolla
  • 2,680
  • 4
  • 14
  • 13
0

It would appear that $.when and then are asynchronous, which means you'll need to handle a callback, probably as a parameter of your function. So for example:

function getConfig(callback){
    $.when(offlinedb.configuration.toArray(function (documents) {
             $.each(documents,function (i,aaa){
                var obj={};
                var temp=aaa.Property;
                var tempObj= aaa.Value;
                obj[temp]=tempObj;
                items.push(obj);
           });
          myJsonString = JSON.stringify(items);
        })).then(function (y,yy){
            callback(myJsonString)
      });
}
// example: getConfig(function(result) { console.log(result); });
Jeff Sisson
  • 1,616
  • 11
  • 22