What is the difference between then and finally in a promise?

First difference: Sometimes you don’t want to catch errors at the place they arise, but in the code that uses this function, so you don’t catch them. In that case you can’t substitute then() and finally(). Sometimes you have to clean something up whether there was an error or not (nulling references, clearing timeouts … … Read more

How do you properly promisify request?

The following should work: var request = Promise.promisify(require(“request”)); Promise.promisifyAll(request); Note that this means that request is not a free function since promisification works with prototype methods since the this isn’t known in advance. It will only work in newer versions of bluebird. Repeat it when you need to when forking the request object for cookies. … Read more

Are nested catches within promises required?

No, they won’t. They only bubble up to the result promise if you chain your promises, for which you need to return the inner promises created by the callbacks. Otherwise the outer promise cannot wait for them and will not know when/how they resolve (whether they fulfill or reject). temporaryUserModel.findOne({email: req.body.email}).then(tempUser => { if (tempUser) … Read more

Define empty Bluebird promise like in Q

Florian provided a good answer For the sake of your original question, there are several ways to start a chain with Bluebird. One of the simplest is calling Promise.resolve() on nothing: var queue = Promise.resolve(); //resolve a promise with nothing or cast a value or Promise.try(function(…){ return …//chain here }); So you can do: var … Read more

Bluebird, promises and then()

Welcome to the wonderful world of promises. How then works in your example Your assertion in 1 is correct. We can simulate a promise resolving in Bluebird using Promise.resolve on a value. Let’s show this: Let’s get a function that returns a promise: function foo(){ return Promise.resolve(“Value”); } foo().then(alert); This short snippet will alert “Value” … Read more

if-else flow in promise (bluebird)

I think you’re looking for (conditionA ? fs.writeFileAsync(file, jsonData) : Promise.resolve()) .then(functionA); which is short for var waitFor; if (conditionA) waitFor = fs.writeFileAsync(file, jsonData); else waitFor = Promise.resolve(undefined); // wait for nothing, // create fulfilled promise waitFor.then(function() { return functionA(); });

tech