Immediately return a resolved promise using AngularJS

后端 未结 5 1108
故里飘歌
故里飘歌 2020-12-10 00:57

I\'m trying to get my head around promises in JavaScript (in particular AngularJS).

I have a function in a service, let\'s call it fooService, that chec

5条回答
  •  被撕碎了的回忆
    2020-12-10 01:19

    You could use the $q.defer() like this:

    this.update = function (data_loaded) {
        var deferred = $q.defer();
    
        if (data_loaded) {
            deferred.resolve(null); // put something that your callback will know the data is loaded or just put your loaded data here.
        } else {
            Restangular.all('someBase').customGet('foo/bar').then(function(data) {
                // Do something here when update is finished
                deferred.resolve(data);
            }
        }
    
        return deferred.promise;
    };
    

    Hope this helps.

提交回复
热议问题