Tag: promise

How to replace Promise.defer with new Promise()

So I see that Promise.defer is now deprecated and we are now supposed to use new Promise instead. I don’t see how to do this however for this example? var Promise = require(‘bluebird’); var interval; var rollDice = function (resolver) { console.log(“rolling”); if (Math.floor(Math.random() * 10) == 7) { clearInterval(interval); resolver.resolve(); } } var rollTill7 […]

Why is this Node snippet not catching rejected Promise?

I’m trying to understand why a Promise rejection exception I’ve encountered is not being handled by my initial try/catch in the following code (full code is in this branch) In this particular error, nodeCf.deploy is being called in index.js. The error is happening because I should be using this.deployName, not stack.deployName in my console.log statement. […]

nodejs readers/writers concurrency

Here’s some simple code that demonstrates what I’m trying to do myVar = 1 reader = () -> getDataFromServer1().then -> # uses myVar and does stuff according to its value # returns promise writer = () -> getDataFromServer2().then -> # assigns to myVar # returns promise Q.all([reader(), reader(), reader(), writer(), writer(), writer()]).done -> console.log ‘done’ […]

How can I trigger a single error handler with nested promises?

Why don’t any of the deferred.reject() calls trigger the .fail() handler at the end? How can I trigger a single error handler with nested promises? I need nested promises for the closure. When I trigger the reject on d1, it still goes all the way to the resolution/rejection of d2. If I add .fail() to […]

Using node.js and promise to fetch paginated data

Please keep in mind that I am new to node.js and I am used with android development. My scenario is like this: Run a query against the database that returns either null or a value Call a web service with that database value, that offers info paginated, meaning that on a call I get a […]

Trouble to synchronise promises in Node.js using Q

I am currently doing an API in Node.JS with the framework Sails.js. I am using promises for the first time and I have some troubles to sync my promises like I want. My main function is the following : createCard: function(req, res) { checkIfUserHasStripeAccount(req.user) .then(addCreditCardToStripeAccount()) .then(function cardCreated() { res.send(200, { msg: ‘Card created’ }); }) […]

Node promise-chaining causes too many return statements in function

Asuming the next reset password function: function forgotPassword(email){ return Promise.resolve().then(function() { return User.findByMail(email); }).then(function(user){ if (!user) { return Promise.reject({message: ‘Cannot find user with that email’}); } return [user, tokensService.createRandomBytes()]; }).spread(function(user, token){ user.resetPasswordToken = token; user.resetPasswordExpires = Date.now() + 3600000; // 1 hour return [user.saveAsync(), token]; }).spread(function(user, token){ return emailService.sendResetPassword(user.email, token); }); } The caller of […]

node program with promises doesn't finish

I cannot for the life of me figure out why this doesn’t finish: var pmongo = require(‘promised-mongo’); var db = pmongo(‘mongodb://localhost/builder’); var block_id = ‘538d097bbb12479d0e9f70ab’; var collection = db.collection(‘block’); collection.findOne({_id:db.ObjectId(block_id)}) .then(function(result) { console.dir(result); }).done(); It bascially just hangs. findOne returns a promise, I’m calling done. Strangely, when I close the database (ie db.close()) in the […]

How to return a Mongoose object from saveAsync?

I’ve promisified Mongoose with Bluebird – all seems to work fine except that documents returned by saveAsync() are just plain objects and lack methods/virtuals. order.saveAsync().then(function(order) { order.save(); // errors with object has no method ‘save’ I am using ‘save’ as an example but no builtin method or my custom methods seem to be available. How […]

Bluebird – how to propagate errors in nested promise

PromiseA().then(function(){ PromiseB().catch(function (e) { throw e; }) }).catch(function (e) { // I want exceptions thrown in the nested promise chain to end up here. }) How do I get exceptions from nested promises to bubble up to the parent promise?

Node.js is the Best Javascript runtime in the world.