If you really need to start an asynchronous call in the process of setting up your provider, you can use $ interval.
this.$get = function($q, $interval) {
var deferred = $q.defer();
var handler = $interval(function() {
if (myThingHasBeenLoaded) {
$interval.cancel(handler);
deferred.resolve(myThingsValue);
}
}, 100);
return {
getMyThing: function() {
return deferred.promise();
}
};
}
myThingService.getMyThing().then(function(myThing) { console.log(myThing); } );
API Google, . 100 , , . , 100 , 1 . , :
angular.module("myModule").provider("AsyncInit", function () {
this.options = {};
this.init = function(options) {
this.options = options;
};
this.$get = function($q) {
var deferred = $q.defer();
var self = this;
return {
getMyThing: function() {
doSomeAsyncStuff(self.options)
.success(function(newThing) {
deferred.resolve(newThing);
})
.failure(function(errors) {
deferred.reject(errors);
});
return deferred.promise();
}
};
};
});
, .