Immediately return a resolved promise using AngularJS

26,502

Solution 1

The current accepted answer is overly complicated, and abuses the deferred anti pattern. Here is a simpler approach:

this.update = function(data_loaded) {
    if (data_loaded) return $q.when(data);  // We've loaded the data, no need to update

    return Restangular.all('someBase').customGet('foo/bar')
                             .then(function(data) {
        // Do something with the data here 
    });
};

Or, even further:

this._updatep = null;
this.update = function(data_loaded) { // cached
    this._updatep = this._updatep || Restangular.all('someBase') // process in
                                                .customGet('foo/bar'); //.then(..
    return this._updatep;
};

Solution 2

As your promise use the same syntax as the JavaScript native one, you could use and return an already resolved JavaScript promise : Promise.resolve()

return(Promise.resolve("MyReturnValue"));

Solution 3

AngularJS's $q service will help you here. It is much like Kris Kowal's Q promise library.

When you have an async method that may return a promise or value use the $q.when method. It will take what ever is passed to it, be it a promise or a value and create a promise that will be resolved/rejected based on the promise passed, or resolved if a value is passed.

$q.when( fooService.update(data_loaded) ).then(function(data){
   //data will either be the data returned or the data
   //passed through from the promise
})

and then in your update function return the data instead of just returning

if (data_loaded) return data_loaded;
Share:
26,502
samturner
Author by

samturner

Updated on July 24, 2020

Comments

  • samturner
    samturner almost 4 years

    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 checks if we've loaded some data. If it has, I just want it to return, and if we haven't, we need to load the data and return a promise:

    this.update = function(data_loaded) {
        if (data_loaded) return;  // We've loaded the data, no need to update
    
        var promise = Restangular.all('someBase').customGet('foo/bar').then(function(data) {
            // Do something with the data here
        }
    
        return promise;
    }
    

    I have another function that then calls the update function of fooService like so:

    fooService.update(data_loaded).then(function() {
        // Do something here when update is finished
    })
    

    My issue here is that if we don't need to load the data in the update function, a promise isn't returned, so the .then() is not called in my other function. What should the approach be here - basically I want to return a resolved promise immediately from the update() function if we do not need to get data from the Restangular call?