Apache Cordova Native Storage return from callback to outer function

Viewed 192

im developing a mobile App with Apache Cordova und want to use the Native Storage plugin to store user data on the devices. I moved from local storage to Nativestorage but just cant get it working on the ios emulator (using macos).

I tried many solutions including Closures but did not get it working sadly...

My code looks like this, i want to call getValue("key") and return the desired value.

getValue: function(key) {
  var value;
  window.NativeStorage.getItem(key,
    function(result) {
      value = result;
      // result holds the value i need
    },
    function(e) {
      // alert("Read Object Failed");
    });


  return value;
  // is undefined
},

This is what i tried but it did not work:

getValue: function(key, callback) {
  var value;
  window.NativeStorage.getItem(key,
    function(result) {
     callback(result)

    },
    function(e) {
      // alert("Read Object Failed");
    });
},

calling it like this:

getValue("key", (function(response) {
alert("response: " + response);
}

which is actually not really what i need because i want to write the return value to a var... Any ideas are highly appreciated

0 Answers
Related