Because no Promise
or other value is return
ed from .then()
chained to Promise
constructor.
Note that .then()
returns a new Promise
object.
The solution is to return
a value or other function call which return
s a value or Promise
from .then()
.
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");
}
// `return` `result` or other value here
// to avoid `undefined` at chained `.then()`
return result
})
}
doStuff(9)
.then(function(data) {
console.log("data is: " + data) // `data` is not `undefined`
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…