javascript 未调用承诺链中的返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32981798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
return value inside promise chain isn't getting called
提问by bdv
I'm using the promise library Bluebird and I'm currently running into the issue that everything inside the function runs great, but when I try to return a value, the function instead returns undefined
.
我正在使用 Promise 库 Bluebird,我目前遇到的问题是函数内部的所有内容都运行良好,但是当我尝试返回一个值时,该函数反而返回undefined
.
This is the promise chain:
这是承诺链:
function foo() {
createGroupMembers(parsedChat).then(function(val) {
var members = val;
createMessages(parsedChat, maxPages).then(function(val) {
var messages = val;
Promise.all([ createFrontCover(subject, firstdateOfMessages, lastDateOfMessages, isPreview), createStats(parsedChat), createBackCover(parsedChat)])
.then(function (results) {
var front = results[0];
var stats = results[1];
var backcover = results[2];
var book = head + front + stats + members + messages + backcover;
console.log('pages in this book: ', pages);
console.log(book); // logs perfect values.
return book;
});
});
});
}
The problem is simple: when calling foo()
, it's value becomes undefined
instead of book. Why am I experiencing this behaviour?
问题很简单:调用时foo()
,它的值变为undefined
而不是 book。为什么我会遇到这种行为?
回答by Jaromanda X
function foo() {
return createGroupMembers(parsedChat).then(function(val) {
var members = val;
return createMessages(parsedChat, maxPages).then(function(val) {
var messages = val;
return Promise.all([createFrontCover(subject, firstdateOfMessages, lastDateOfMessages, isPreview), createStats(parsedChat), createBackCover(parsedChat)])
.then(function(results) {
var front = results[0];
var stats = results[1];
var backcover = results[2];
var book = head + front + stats + members + messages + backcover;
console.log('pages in this book: ', pages);
console.log(book); // logs perfect values.
return book;
});
});
});
}
Now foo will return a promise which can resolve to the value of book
现在 foo 将返回一个可以解析为 book 值的 promise
foo().then(function(book) {
console.log('huzzah I have book ' + book);
});
To be honest, foo
could be rewritten, but that's a different question altogether
老实说,foo
可以重写,但那完全是一个不同的问题
FYI: you could do something like this for foo
仅供参考:你可以为 foo 做这样的事情
function foo() {
return createGroupMembers(parsedChat)
.then(function(members) { // members
return Promise.all([members, createMessages(parsedChat, maxPages)]);
})
.then(function(members_messages) { // membersMessages
return Promise.all([createFrontCover(subject, firstdateOfMessages, lastDateOfMessages, isPreview), createStats(parsedChat)].concat(members_messages, [createBackCover(parsedChat)]));
})
.then(function(results) { // front, stats, members, messages, back
var book = head + results.join('');
console.log('pages in this book: ', pages);
console.log(book); // logs perfect values.
return book;
});
}
Messed around with the order in the second (was your only) Promise.all, and added the previous Promise results in it to make the final conatenation of parts as simple as a .join
- doing it this way will also propagate any erros correctly, so your usage of foo can be
弄乱了第二个(是你唯一的)Promise.all 中的顺序,并在其中添加了之前的 Promise 结果,使部分的最终连接变得如此简单.join
- 这样做也将正确传播任何错误,因此您的foo 的用法可以是
foo().then(function(book) {
console.log('huzzah I have book ' + book);
}).catch(function(err) {
// handle any and all errors here
});