Question

I saw following code in the HotTowel project. In the following code, callback method for then returns value return vm.messabeCount = data;

(function () {
    'use strict';
    function dashboard(common, datacontext) {
        vm.messageCount = 0;

        function getMessageCount() {
            return datacontext.getMessageCount().then(function (data) {
                /******* Here ********/
                return vm.messageCount = data;
            });
        }
    }
})();

Am wondering why & to whom it's returning value. Is it some standard practice? Can't the code be just.

return datacontext.getMessageCount().then(function (data) {
    vm.messageCount = data;
});

Or

return datacontext.getMessageCount().then(function (data) {
    vm.messageCount = data;
    return;
});
Was it helpful?

Solution

getMessageCount is a function returning a promise object. then method of this promise returns another promise again. It makes possible to chain multiple then parts. Each then(function() { ... }) has an ability to modify a data to be passed to the next then invocation. So this construction:

return datacontext.getMessageCount().then(function(data) {
    return vm.messageCount = data;
});

means to modify a data passed to resolve callbacks. Without this return success functions would be resolved with undefined value, while we need it to be resolved with data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top