q

What is the explicit promise construction antipattern and how do I avoid it?

China☆狼群 提交于 2019-11-25 23:55:36
问题 I was writing code that does something that looks like: function getStuffDone(param) { | function getStuffDone(param) { var d = Q.defer(); /* or $q.defer */ | return new Promise(function(resolve, reject) { // or = new $.Deferred() etc. | // using a promise constructor myPromiseFn(param+1) | myPromiseFn(param+1) .then(function(val) { /* or .done */ | .then(function(val) { d.resolve(val); | resolve(val); }).catch(function(err) { /* .fail */ | }).catch(function(err) { d.reject(err); | reject(err

Issue in returning data retrieved from DB queries called in the loop

允我心安 提交于 2019-11-25 23:31:54
问题 I making multiple mongoDB queries in loop. and want to send the all results as one data array.But when I simple use the return for send the data it simply return undefined and do not wait for results of all DB request. I also tried to use q.moulde but same issue. Code: var getPrayerInCat = function(data){ var result ; var finalData = []; if(data.length >0){ data.forEach(function(data2){ var id= data2.id; Prayer.find({prayerCat:id},function(err,prayer){ var deferred = Q.defer() if (err) { // .

Problems inherent to jQuery $.Deferred (jQuery 1.x/2.x)

自作多情 提交于 2019-11-25 23:22:25
问题 @Domenic has a very thorough article on the failings of jQuery deferred objects: You\'re missing the Point of Promises. In it Domenic highlights a few failings of jQuery promises in comparison to others including Q, when.js, RSVP.js and ES6 promises. I walk away from Domenic\'s article feeling that jQuery promises have an inherent failing, conceptually. I am trying to put examples to the concept. I gather there are two concerns with the jQuery implementation: 1. The .then method is not

Resolve promises one after another (i.e. in sequence)?

大城市里の小女人 提交于 2019-11-25 21:57:04
问题 Consider the following code that reads an array of files in a serial/sequential manner. readFiles returns a promise, which is resolved only once all files have been read in sequence. var readFile = function(file) { ... // Returns a promise. }; var readFiles = function(files) { return new Promise((resolve, reject) => var readSequential = function(index) { if (index >= files.length) { resolve(); } else { readFile(files[index]).then(function() { readSequential(index + 1); }).catch(reject); } };