3

i am creating android application using ionic and angularjs and using window.requestFileSystem for get image from storage. So for this process i used $q.defer() for asynchronous process and its working fine and showing object in console: console.log(deferred.promise)

enter image description here

but now, when i am trying to get the value from deferred.promise like

var vals = deferred.promise;
console.log(vals.$$state.value);

its showing undefined. Can anyone tell me how i can get that value?

My Code:

var deferred = $q.defer();
    document.addEventListener("deviceready", function() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
            imagePath = fileSystem.root.toURL() + 'SmaartMedia/' + splited[splitedLength-1];
            deferred.resolve(imagePath);
            $rootScope.$apply();
        });
    }, false);
    var vals = deferred.promise;
    console.log(vals.$$state.value);

Thanks

1 Answer 1

4
function getStuff() {
  var deferred = $q.defer();
  document.addEventListener("deviceready", function () {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
      imagePath = fileSystem.root.toURL() + 'SmaartMedia/' + splited[splitedLength - 1];
      deferred.resolve(imagePath);
      $rootScope.$apply();
    });
  }, false);
  return deferred.promise;
}

//Get value like this
getStuff().then(function(response){
  //Use value here
});
Sign up to request clarification or add additional context in comments.

1 Comment

Not correct, look at the eventListener. defer should be inside. And i also don't think it's really working, because the eventListener will not be executed in this function, it only get registered. Nothing more. So the return won't work either. There is no nice way to achieve his goal, because of the eventListener.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.