这是我的第一篇文章,如果做错了,抱歉。
我正在尝试将javascript Promise链接在一起,但是当我运行它时,出现错误Uncaught TypeError:无法读取未定义的属性'then'在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);
})
要么兑现承诺:
const orderToys = () => {
return new Promise((resolve, reject) => {
if (crates.toys < 60) {
resolve('yes');
} else {
reject('no');
}
})
}
或使用箭头函数的表达形式。
const orderToys = () =>
new Promise((resolve, reject) => {
if (crates.toys < 60) {
resolve('yes');
} else {
reject('no');
}
});
您没有从函数返回承诺。您需要先返还诺言,然后才能尝试解决它们。希望这会有所帮助。
const crates = {
clothes: 8,
hats: 20,
toys: 50,
}
const orderToys = () => {
return new Promise((resolve, reject) => {
if (crates.toys < 60){
resolve('yes');
} else {
reject('no');
}
})
}
const sendMoney = (val) => {
return new Promise((resolve, reject) => {
if(val === 'yes'){
resolve('money sent');
} else {
reject('no order needed, do not sent money');
}
})
}
const orderConfirmed = (val) =>{
return new Promise((resolve, reject) => {
if(val ==='money sent'){
resolve('shipped');
} else {
reject('No money sent');
}
})
}
const delivered = (val) =>{
return 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);
})
如果您使用的是[[Promises,那么您[还应返回诺言,以保留诺言链:
const crates = {
clothes: 8,
hats: 20,
toys: 50,
};
const orderToys = () => {
return new Promise((resolve, reject) => {
if (crates.toys < 60) {
resolve('yes');
} else {
reject('no');
}
});
};
const sendMoney = (val) => {
return new Promise((resolve, reject) => {
if (val === 'yes') {
resolve('money sent');
} else {
reject('no order needed, do not sent money');
}
});
};
const orderConfirmed = (val) => {
return new Promise((resolve, reject) => {
if (val === 'money sent') {
resolve('shipped');
} else {
reject('No money sent');
}
});
};
const delivered = (val) => {
return 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);
});
或者如果您需要使用异步等待形式:
(async function () {
try {
const value1 = await orderToys();
const value2 = await sendMoney(value1);
const value3 = await orderConfirmed(value2);
await delivered(value3);
} catch (e) {
// Handle errors
console.log(e);
}
})();