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;
});
}