q

When should I use Q.defer and when just Promise.resolve/reject?

扶醉桌前 提交于 2019-12-11 10:05:26
问题 I'm using nodejs and was wondering when should I use Q defer, and when just use Promise.resolve/reject? I saw a lot of examples of both kinds, for example: // with Q defer fucntion oneWay(myVal) { var deffered = Q.defer(); if (myVal < 0) { deffered.reject(new Error('nope')); } else { deffered.resolve('yay'); } return deffered.promise; } // native Promise fucntion orAnother(myVal) { if (myVal < 0) { return Promise.reject(new Error('nope')); } else { return Promise.resolve('yay'); } } What's

Q.js progress handler doesn't seem to be firing

∥☆過路亽.° 提交于 2019-12-11 09:10:42
问题 I'm currently using Q.js for promises, since they implement progress handlers. However, it doesn't seem like they're firing. What am I doing wrong? It seems pretty basic, so I must be missing something. (The example below is written in coffeescript) Q = require('q') squares = (list) -> deferred = Q.defer() result = list.map (e) -> r = e * e deferred.notify(r) return r deferred.resolve(result) return deferred.promise squares([1,2,3,4,5,6,7,8,9,10]) .then((result) -> console.log result )

Javascript & promises with Q - closure problems in promises

北战南征 提交于 2019-12-11 06:18:10
问题 I'm using Node.js and Q to write server-side asynchronous code. I'm new to promises (and I'm new to asynchronous programming in general), and I'm having a bit of trouble that I haven't been able to solve by staring at the Q documentation. Here's my code (it's coffeescript - let me know if you want to see the javascript instead): templates = {} promises = [] for type in ['html', 'text'] promises.push Q.nfcall(fs.readFile , "./email_templates/#{type}.ejs" , 'utf8' ).then (data)-> # the problem

Calling promise in another promise, conditionally

穿精又带淫゛_ 提交于 2019-12-11 05:13:55
问题 This is basically my code, using q: let d = Q.defer(); let result = { name: 'peter' }; d.resolve(result); return d.promise; However, I now need to perform a step based on a certain condition. This step is calling another object that also returns a promise. So I'm having nested promises, if that's the correct term. Something like this: let d = Q.defer(); let result = { name: 'peter' }; if (someParameter) { otherService.getValue() // Let's say it returns 'mary' .then((res) => { result.name =

Log all reject promises in Q

折月煮酒 提交于 2019-12-11 03:15:28
问题 Is there a way to configure Q to log or call a specific function on all rejected promises (like an interceptor)? Many exceptions are being swallowed in my application, and put error handling in all my promises just for logging purposes would be duplicated work to do. Thanks! 回答1: Q actually already supports this - as of 1.3.0 Q offers the standard unhandled rejection hooks: process.on("unhandledRejection", function(reason, p) { console.log("Unhandled rejection detected ", reason, p); }); You

Q.spread and pyramid of doom

筅森魡賤 提交于 2019-12-11 01:34:13
问题 I have this code: Q.spread([ Q.nfcall(employee.save.bind(employee)), ],function(emp){ Q.spread([Q.nfcall(dept.save.bind(dept))],function(dept){ console.log("success") },function(e){ console.error(e); mongoose.disconnect(); }) },function(e){ console.error(e); mongoose.disconnect(); }) Although it works great, it starts to look like the pyramid of doom. Is there a way to refactor it to be more "promising"? I expected something like this to be working: Q.spread([ Q.nfcall(employee.save.bind

Using the $q implementation in Angular how can I loop a promise until success?

ぃ、小莉子 提交于 2019-12-11 01:08:59
问题 As per my other recent questions, I'm trying to persist data to a server for an angular application that's targeted at mobile devices (unstable connections), so it should keep trying the request until success. How can I do this with promises? At the moment I've got: Service: this.addObject = function addObject(object) { var deferred = $q.defer(); var httpConfig = { method: 'POST', url: 'http://api.foo.com/bar', data: object } setTimeout(function() { $http(httpConfig). success(function(data,

javascript node.js in a stand-alone script, block/wait on a promise

落爺英雄遲暮 提交于 2019-12-11 00:30:27
问题 I have a simple program in node.js, such as: // CODE1 step1(); step2(); var o = step3(); step4(o); step5(); step6(); this program is meant to be run in a stand-alone script (not in a web browser), and it is a sequential program where order of execution is important (eg, step6 needs to be executed after step5). the problem is that step3() is an async function, and the value 'o' is actually passed to a given callback, so I would need to modify the program as follows: // CODE2 step1(); step2();

What does “Promise fires on the same turn of the event loop” mean?

戏子无情 提交于 2019-12-10 23:03:13
问题 New to NodeJS. Going through the promise tutorial ('promise-it-wont-hurt') I have the following script: var Q = require('q'); var deferred = Q.defer(); deffered.resolve('SECOND'); deffered.promise.then(console.log); console.log('FIRST'); The output: FIRST SECOND I don't get it, I would have thought that since resolved is fired first I should see second first. They explain that this happens because of 'Promise fires on the same turn of the event loop'. I don't understand what that means... 回答1

How does one use Q.all with chai-as-promised?

回眸只為那壹抹淺笑 提交于 2019-12-10 21:43:10
问题 The chai-as-promised docs have the following example of dealing with multiple promises in the same test: it("should all be well", function (done) { Q.all([ promiseA.should.become("happy"), promiseB.should.eventually.have.property("fun times"), promiseC.should.be.rejectedWith(TypeError, "only joyful types are allowed") ]).should.notify(done); }); I assume that the Q here has come from npm install q and var Q = require('q'); . Where does .should come from? When I try this should is undefined