javascript - deferred anti pattern of promises unclear example -


from

https://github.com/petkaantonov/bluebird/wiki/promise-anti-patterns

what promise talking about?

myapp.factory('configurations', function (restangular, motorrestangular, $q) {     var getconfigurations = function () {         //just return promise have!         return motorrestangular.all('motors').getlist().then(function (motors) {             //group cofig             var g = _.groupby(motors, 'configuration');             //return mapped array value of promise             return _.map(g, function (m) {                 return {                     id: m[0].configuration,                     configuration: m[0].configuration,                     sizes: _.map(m, function (a) {                         return a.sizemm                     })                 }             });         });     };      return {         config: getconfigurations()     }  }); 

where promise? me looks more it's anti pattern use pattern. cannot see promise in code apart word then nothing makes me think of promise.

so return motorrestangular... return?

one thing remember that, both resolve , reject function return promise. in other word, it's promisified you. if don't explicitly return resolve, promise has been resolved value of undefined.

in example, motorrestangular.all('motors').getlist() returns promise , in resolve function, first param in then, resolved promise returned result of _.map function input. it's like:

function yourctrl(configurations) {     configurations.getconfiguration().then(function(resultofmap) {         // use resultofmap here     }) } 

note: don't confused resolved , onfulfilled. kinda same former deferred object , latter promise specification.


Comments

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

html - Outlook 2010 Anchor (url/address/link) -

android - How to create dynamically Fragment pager adapter -