Q deferreds and requestFileSystem (AngularJS)

I'm encountering some problems when trying to resolve a deferred in the following situation

services.factory('MyService', ['$q',
 function($q) {
  var Foo = function() {
    var deferred = $q.defer();
    window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
    window.requestFileSystem(window.TEMPORARY, 1024 * 1024, deferred.resolve, this.errorHandler);
    return deferred.promise;
  }

  return {
    Foo: Foo
  }
 }
]);

Then inside a controller:

var bar = new MyService.Foo().then(function(cb) {
  console.log(cb)
});

If I use deferred.resolve outside requestFileSystem function then it works perfectly but in the situation above nothing happens. PS: the third parameter to requestFileSystem function is a callback which gets a filesystem object once it's ready.

EDIT: this is bar:

Object {then: function, catch: function, finally: function}

the fourth parameter to requestFileSystem is a function which logs to console filesystem API initialization errors but in this case it's not called because requestFileSystem is successful so deferred.resolve should be called. if i replace deferred with a normal function:

 window.requestFileSystem(window.TEMPORARY, 1024 * 1024, function(filesystem){
   console.log(filesystem);
 }, this.errorHandler);

then filesystem object is returned correctly:

DOMFileSystem {root: DirectoryEntry, name: "http_127.0.0.1_3000:Temporary"}

EDIT2:

I tried to make a simple test by simulating requestFilesystem function with my own function and it works:

var Foo = function() {
    var deferred = $q.defer();
    asd(deferred.resolve)
    return deferred.promise;
}

function asd(callback) {
    callback('it works')
}

MyService.Foo().then(function(cb){
  console.log(cb)
})

The basic problem is how and when deferred.resolve is invoked.
Fixed code:

services.factory('MyService', ['$q','$rootScope',
 function($q,$rootScope) {
  var Foo = function() {
    var deferred = $q.defer();
    window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
    window.requestFileSystem(window.TEMPORARY, 1024 * 1024, function(result){
      deferred.resolve(result);
      $rootScope.$apply();//force angular to resolve promise
    }, this.errorHandler);
    return deferred.promise;
  }

  return {
    Foo: Foo
  }
 }
]);

Thing is, when you invoke 'deferred.resolve' outside of angular lifecycle (callback of filesystem request is outside of angular lifecycle), you have to manually invoke '$rootScope.$apply()' otherwise promise won't be resolved.

There's a better solution from github.com/maciel310/angular-filesystem

//wrap resolve/reject in an empty $timeout so it happens within the Angular call stack
//easier than .apply() since no scope is needed and doesn't error if already within an apply
function safeResolve(deferral, message) {
    $timeout(function() {
        deferral.resolve(message);
    });
}
function safeReject(deferral, message) {
    $timeout(function() {
        deferral.reject(message);
    });
}