TheCocoaProject / cordova-plugin-nativestorage

Cordova plugin: Native storage of variables in Android, iOS and Windows
http://thecocoaproject.github.io/
Apache License 2.0
292 stars 106 forks source link

How can i get value from NativeStorage.getString() Without Using Callbacks? #144

Open elkhalifte opened 5 years ago

elkhalifte commented 5 years ago

I Have Used This Plugin cordova-plugin-nativestorage in My Phonegap app . The Plugin Works fine.

I Wonder If There Is any trick to get values from Nativestorage.getItem() without callbacks

This My Code i Call app.getstring('ref_string'); To get Saved Data in ref_string Key as an Alert.

When I Try To save it To Input Text as a Value I Got Undefined.

getstring:function(key){

ativeStorage.getString(key,function (result) {

      alert('String Is '+result);},

         function (e) {

      alert('Error '+e);});
}
J3m5 commented 5 years ago

The solution would be to wrap the methods in a promise. That would make them asynchronous.

const getItem = key => 
  new Promise((resolve, reject) => NativeStorage.getItem(key, resolve, reject))

// in an async function
const value = await getItem("key");

I didn't test it, but it's certainly the way to go.