There are many differences between the two approaches.
The main difference between the two fragments is that in version 2 you implicitly pass the allowed value from foo directly to bar . In addition to this, doSomething allow everything that bar will be allowed, whereas in version 1 result will be discarded.
Some additional important points made by Benjamin Grunbaum:
(a) if the bar is a link error 1 rejects the internal promise and 2 throws.
(b) 1 requires a reference to $ q, where 2 is an agnostic implementation.
(c) version 1 is not a safe exception , and failure will be swallowed where version 2 will allow you .; There are a few smaller differences. See stackoverflow.com/questions/22539815
You can also write it like this. Thus, you will not get an implicit pass-through of the allowed value from foo to bar (now it is explicit), which can be confusing or easily overlooked. It can also be useful if you want to do something with the allowed values โโof foo or bar before returning them.
function doSomething() { return foo().then(function(res){ // maybe do something with the result of foo return bar(res); }).then(function(res){ // maybe do something with the result of bar return res; }); }
Manually creating a pending object should be minimized and is usually an anti-pattern.
https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns#the-deferred-anti-pattern
The basic principle demonstrated here is that the promise will assume the state of the returned promise (or subsequent) within it of the solution method.
Willem d'haeseleer
source share