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

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

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -