以下是我的代码的链接:https://jsbin.com/pijakayage/edit?js,console
这是触发错误的函数
var willIGetNewPhone = () => {
return new Promise((resolve, reject)=>{
if(isMomHappy){
var phone = {
brand: 'Samsung',
color: 'black'
};
resolve(phone);
}else {
var reason = new Error('mom is not happy');
reject(reason);
}
});
}
我真的没有得到第一个评论函数和第二个评论函数之间的区别!在第二个函数我只是定义一个返回一个promise的函数!为什么它不起作用?!为什么我得到:" willIGetNewPhone.then不是一个功能" ...
谢谢你们的帮助..
答案 0 :(得分:0)
因为willIGetNewPhone
是一个函数
问题是
// call our promise
var askMom = function () {
willIGetNewPhone
.then(showOff) // chain it here
.then(function (fulfilled) {
console.log(fulfilled);
// output: 'Hey friend, I have a new black Samsung phone.'
})
.catch(function (error) {
// oops, mom don't buy it
console.log(error.message);
// output: 'mom is not happy'
});
};
应该是
// call our promise
var askMom = function () {
willIGetNewPhone()
.then(showOff) // chain it here
.then(function (fulfilled) {
console.log(fulfilled);
// output: 'Hey friend, I have a new black Samsung phone.'
})
.catch(function (error) {
// oops, mom don't buy it
console.log(error.message);
// output: 'mom is not happy'
});
};
willIGetNewPhone()
代替willIGetNewPhone