【发布时间】:2017-06-08 15:15:12
【问题描述】:
给定
function doStuff(n /* `n` is expected to be a positive number */) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(n * 10)
}, Math.floor(Math.random() * 1000))
})
.then(function(result) {
if (result > 100) {
console.log(result + " is greater than 100")
} else {
console.log(result + " is not greater than 100");
}
})
}
doStuff(9)
.then(function(data) {
console.log(data) // `undefined`, why?
})
为什么data undefined .then() 链接到doStuff() 调用?
【问题讨论】:
-
这个问题/答案的目的是canonical。有几个问题,这个特定的模式,它,
returning 函数调用中的值或.then()的遗漏一直是问题,包括Run JavaScript promises in order. One after the other endspromise标记 efficianodos 是否加入,如果愿意的话。 -
@washington 猜测这是一个规范的答案。如果您找到一个真正好的,则仅将其标记为重复。 (不像试试这个)
标签: javascript promise