admin管理员组文章数量:1290980
I thought it was supposed to be possible to chain the .then() method when using ES6 Promises. In other words I thought that when a promise is resolved the value passed to the resolve function should be passed to any chained then handlers. If this is so how e value es back undefined in the chained then handler below?
function createPromise() {
return new Promise((resolve) => {
resolve(true);
});
}
createPromise()
.then((value) => {
console.log(value); // expected: true, actual: true
})
.then((value) => {
console.log(value); // expected: true, actual: undefined
});
I thought it was supposed to be possible to chain the .then() method when using ES6 Promises. In other words I thought that when a promise is resolved the value passed to the resolve function should be passed to any chained then handlers. If this is so how e value es back undefined in the chained then handler below?
function createPromise() {
return new Promise((resolve) => {
resolve(true);
});
}
createPromise()
.then((value) => {
console.log(value); // expected: true, actual: true
})
.then((value) => {
console.log(value); // expected: true, actual: undefined
});
Share
Improve this question
asked Jan 9, 2017 at 20:20
david_i_smithdavid_i_smith
2283 silver badges12 bronze badges
1
-
You're not returning anything for the chained
.then()
to use as the value. – Jes Commented Jan 9, 2017 at 20:24
3 Answers
Reset to default 9Each then()
can return a value that will be used as the resolved value for the next then()
call. In your first then()
, you don't return anything, hence value
being undefined in the next callback. Return value
in the first one to make it available in the second.
function createPromise() {
return new Promise((resolve) => {
resolve(true);
});
}
createPromise()
.then((value) => {
console.log(value); // expected: true, actual: true
return value;
})
.then((value) => {
console.log(value); // expected: true, actual: true
});
You have to return it inside a promise to pass it along. Or create a new promise that resolves with it.
createPromise()
.then((value) => {
return value;
})
.then((value) => {
console.log(value);
});
Or
createPromise()
.then((value) => {
return new Promise.resolve(value);
})
.then((value) => {
console.log(value);
});
.then
always returns a Promise that resolves to the value returned in the function callback. Since you return nothing in the first call to .then
, undefined
bees the resolved value of the Promise returned.
In other words, if you want to resolve true
in the second call to .then
, you'll have to return it in the first, like so,
createPromise() // returns a Promise
.then(function (value) {
console.log(value); // => true
return value; // now this will return a Promise that resolves the value `true`
}).then(function (value) {
console.log(value) // => true
});
You can refer to MDN's documentation on method chaining for ES2015 promises if you need further information.
本文标签: javascriptChaining then() calls in ES6 promisesStack Overflow
版权声明:本文标题:javascript - Chaining .then() calls in ES6 promises - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741524055a2383360.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论