【发布时间】:2023-03-23 23:55:02
【问题描述】:
这是我的第一篇文章,如果我做错了,请见谅。
我正在尝试将 javascript 承诺链接在一起,但是当我运行它时,我收到错误 Uncaught TypeError: Cannot read property 'then' of undefined 在 script.js:108 (匿名)@script.js:108
你知道我做错了什么吗?
const crates = {
clothes: 8,
hats: 20,
toys: 50,
}
const orderToys = () => {
new Promise((resolve, reject) => {
if (crates.toys < 60){
resolve('yes');
} else {
reject('no');
}
})
}
const sendMoney = (val) => {
new Promise((resolve, reject) => {
if(val === 'yes'){
resolve('money sent');
} else {
reject('no order needed, do not sent money');
}
})
}
const orderConfirmed = (val) =>{
new Promise((resolve, reject) => {
if(val ==='money sent'){
resolve('shipped');
} else {
reject('No money sent');
}
})
}
const delivered = (val) =>{
new Promise((resolve, reject) => {
if(val === 'shipped'){
resolve('order completed');
} else {
reject('no order');
}
})
};
orderToys()
.then((firstVal) => {
return sendMoney(firstval);
})
.then((secondVal) =>{
return orderConfirmed(secondVal);
})
.then((thirdVal) =>{
return delivered(thirdVal);
})
.then((fourthVal) =>{
console.log(fourthVal);
})
【问题讨论】:
-
你需要
return你的承诺
标签: javascript promise chaining