vendredi 14 août 2015

How to use asynchronous method in a synchronous method

I developed a game in web technology that store the data in LocalStorage. The LocalStorage works as synchronous.To set and get the values from localstorage as

// set the value in localstorage

localStorage.setItem('Key', "value");

// get the value from localstorage

var localValue = localStorage.getItem('Key');

There is 5MB limit in localstorage. After play the some level, localstorage filled completely. I need a large area for storage at client side.

My first question is that “What is the best way to solve this problem?”

To solve above problem,I find the LargeLocalStorage.It is worked as asynchronous.

// set the value in LargeLocalStorage

storage.setContents('docKey', "the contents...").then(function() {alert('doc created/updated');});

// get the value from LargeLocalStorage

storage.getContents('myDoc').then(function(content){//content is value for particular key});

Is there is any way that make these method synchronous because my all code is according to the synchronous localstorage. It will take a lot of time to change the whole logic according to the asynchronous LargeLocalStorage.

I am currently try to override the setItem and getItem method of localstorage as :

localStorage.__proto__.getItem = function(key) {

var returnVal;

storage.getContents(key).then(function(content) {
        returnVal = content;

    });
return returnVal;//it returns undefined
}

Above override method returns undefined value due to asynchronous behaviour.

Please help me to get value synchronous using above methods.

Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire