Problem
What’s the distinction between:
and this:
I’m wondering because when I use Angular and the $http service with chaining, I get various results (). Because there was a little too much code, I started with the example above.
Asked by spirytus
Solution #1
In a nutshell, within a then handler function:
A) When x is an integer, string, or other value:
B) When x is a Promise that has already been fulfilled (it is no longer pending):
C) When x is a Promise that is pending:
The Promise.prototype.then() documentation has more information on this topic.
Answered by Arian Acosta
Solution #2
The rule is that if the function in the then handler returns a value, the promise resolves/rejects with that value, and if the function returns a promise, the next then clause is the then clause of the promise the function returned, so in this case, the first example falls through the normal sequence of the thens and prints out values as one might expect, and in the second example, the promise object returned when you do Promise.resolve(” (for all intents and purposes). More information about how it works can be found further down.
According to the Promises/A+ specification:
This is the line to pay attention to:
Answered by Hrishi
Solution #3
Both of your instances should act in a similar manner.
The resolution value of the promise received from a then() handler is the value returned inside the then() handler (). If the value returned by.then is a promise, the promise returned by then() will “adopt the state” of the returned promise and resolve/reject in the same way as the returned promise.
You return “bbb” in the first then() handler in your first example, therefore “bbb” gets sent to the second then() handler.
In the second example, you return a promise with the value “bbb” that is instantly resolved, so “bbb” is handed to the next then() handler. (The Promise.resolve() method is unnecessary here.)
The end result is the same.
We can tell you why something is happening if you can show us an example of other conduct.
Answered by JLRishe
Solution #4
You’ve already received a satisfactory formal response. I decided I’d throw in a quick one.
The following items are the same as Promises/A+.
As a result, for a promise or plain value X, the following are all the same:
Promise.resolve(x);
new Promise(function(resolve, reject){ resolve(x); });
Promise.resolve().then(function(){ return x; });
Promise.all([x]).then(function(arr){ return arr[0]; });
It’s no surprise that the promises specification is built on the Promise Resolution Procedure, which facilitates interoperability across libraries (such as $q and native promises) and simplifies your life in general. When a promise is kept, a resolution is made, resulting in general consistency.
Answered by Benjamin Gruenbaum
Solution #5
The only distinction is that when you return Promise, you’re making an unneeded promise. resolve(“bbb”). Promise resolution begins when a onFulfilled() handler returns a promise. Promise chaining works in this way.
Answered by vkarpov15
Post is based on https://stackoverflow.com/questions/27715275/whats-the-difference-between-returning-value-or-promise-resolve-from-then