Create an AngularJS promise with no return value

metacubed picture metacubed · Sep 27, 2014 · Viewed 7.8k times · Source

I am using $q to wrap a promise around a legacy callback. However, the existing callback doesn't have a value to return. It takes a success function with no parameters.

angular.module('MyModule').service('MyService', function() {
    function initialize() {
        var deferred = $q.defer();
        LegacyFactory.initialize(
            // 'void' Success Callback
            function () {
                deferred.resolve (/* WHAT DO I PUT HERE? */);
            },
            // Error Callback
            function (errorCode) {
                deferred.reject(errorCode);
            });
        return deferred.promise;
    }
});

I can't find a void version of resolve in the AngularJS docs. I can return a dummy value, but then clients might access that dummy value, which would cause confusion.

How do I create an AngularJS promise with no return value?

NOTE: The question AngularJS promise returning empty object is completely different. That question does return a value in the resolve function.

Answer

Reactgular picture Reactgular · Sep 27, 2014

There is no void data type in JavaScript. Instead JavaScript uses the primitive type undefined that is used to represent a variable that has not been assigned a value.

A method or statement that has no return value will return undefined. A function that doesn't use the return statement will return undefined. An argument that isn't passed to a function will be undefined.

I hope you're starting to see the consistent behavior here of undefined.

function foo(x) { console.log(x); }
foo(); // will print undefined
function zoom() {}
console.log(zoom()); // will print undefined

So when you use deferred.resolve() you are passing undefined as the value for the data argument.

To more specifically answer your question.

"How do I create an AngularJS promise with no return value?"

To write JavaScript code that gives the intent of no return value for the promise. You would write this.

deferred.resolve(undefined);

That makes it clear that there is no intended data.

Later in your callback you don't have to define the data argument, but if you want you can.

 foo().then(function(data){
     if(typeof data === 'undefined') {
          // there is no data
     } else {
          // there is data
     });

if you always expect no data, then just do this.

foo().then(function(){
   // handle success
});