鉴于
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()
原文由 guest271314 发布,翻译遵循 CC BY-SA 4.0 许可协议
Because no
Promise
or other value isreturn
ed from.then()
chained toPromise
constructor.请注意
.then()
返回一个新的Promise
对象。该解决方案
Promise
return
一个值或其他函数调用,该调用.then()
return