admin 管理员组文章数量: 1184232
Doing some research about Promises and I know that a Promise object can be in three states (pending, resolved, rejected). The logic responsible for each of these three states is in a callback function which gets passed into the Promise constructor. This callback function has 2 functions as its arguments resolve and reject which are called when this callback either results in success or failure.
After the Promise is instantiated we can then add response handler callbacks to the promise by calling the .then function on it. The .then function takes 2 callback functions as its arguments. The first argument is the callback function is called in case the Promise resolve function is called and the second callback is called in case the Promise reject function is called. You can also call .catch on Promises to handle the rejected promises although this is just syntactic sugar for:
.then(undefined, () => { failure callback})
What I find harder to understand is the fact that the .then method returns a Promise. For example in the following code:
Example
let random = (Math.random() * 10);
let promise = new Promise((res, rej) => {
if (random >= 5) {
res(random);
}
rej(random);
});
promise
.then(
(nr) => {
console.log("succes: " + nr);
return nr + 5;
})
.then((nr) => {
console.log(nr);
})
.catch(
(nr) => {
console.log("failure: " + nr);
})
Doing some research about Promises and I know that a Promise object can be in three states (pending, resolved, rejected). The logic responsible for each of these three states is in a callback function which gets passed into the Promise constructor. This callback function has 2 functions as its arguments resolve and reject which are called when this callback either results in success or failure.
After the Promise is instantiated we can then add response handler callbacks to the promise by calling the .then function on it. The .then function takes 2 callback functions as its arguments. The first argument is the callback function is called in case the Promise resolve function is called and the second callback is called in case the Promise reject function is called. You can also call .catch on Promises to handle the rejected promises although this is just syntactic sugar for:
.then(undefined, () => { failure callback})
What I find harder to understand is the fact that the .then method returns a Promise. For example in the following code:
Example
let random = (Math.random() * 10);
let promise = new Promise((res, rej) => {
if (random >= 5) {
res(random);
}
rej(random);
});
promise
.then(
(nr) => {
console.log("succes: " + nr);
return nr + 5;
})
.then((nr) => {
console.log(nr);
})
.catch(
(nr) => {
console.log("failure: " + nr);
})
Question:
In the example at the first .then it returns : nr + 5. In resolve cases of the Promise this value is succesfully passed onto the second .then. How is this possible? Is it under the hood:
return new Promise((res,rej) => {
res(nr + 5)
})
or is this caused by something else?
Share Improve this question asked Feb 11, 2018 at 13:26 Willem van der VeenWillem van der Veen 36.6k18 gold badges205 silver badges176 bronze badges3 Answers
Reset to default 5It is the behaviour of a promise, it is described here
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
In Return value section:
if the handler function returns a value, the promise returned by
thengets resolved with the returned value as its value;
Yes, this one of major features of promises: they are chainable.
The then method does return a new promise that will be resolved with the result of the callback. It does indeed construct a new Promise and does call resolve with the return value when the callback will be called.
You might want to take a look at this toy implementation to see how it is implemented (without the error handling, though).
Look at this: Promises chaining
Normally, a value returned by a .then handler is immediately passed to the next handler. But there’s an exception.
If the returned value is a promise, then the further execution is suspended until it settles. After that, the result of that promise is given to the next .then handler.
Basically, when you return a value, i.e: (nr + 5) or 5 or [1, 2] or {a: 1}, Etc., that value is passed immediately to the next handler (.then).
本文标签: nodejsJavascript Promisethen return valuesStack Overflow
版权声明:本文标题:node.js - Javascript, Promise.then return values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1740028470a2121182.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论