英文:
I have a problem with UnhandledPromiseRejection don't know how to solve it
问题
我正在跟随这段代码的课程部分,但遇到了这个错误:
这个错误是由于在异步函数中抛出异常但没有捕获,或者拒绝了一个没有用 .catch() 处理的 promise 导致的。拒绝的 promise 带有原因 "该商品已售罄。"。] {
code: 'ERR_UNHANDLED_REJECTION'
}
这是代码:
const inventory = {
sunglasses: 0,
pants: 1088,
bags: 1344
};
// 在下面编写你的代码:
const myExecutor = (resolve, reject) =>{
if(inventory.sunglasses > 0){
resolve('太阳镜订单已处理。');
}else{
reject('该商品已售罄。');
}
}
const orderSunglasses = () => {
return new Promise(myExecutor);
}
const orderPromise = orderSunglasses();
console.log(orderPromise);
我尝试在线查找错误并阅读其他人的错误,但不太清楚,但我一点都不清楚,因为我是JavaScript的新手。
英文:
I'm following a course section with this code and gives me this error:
This error originated either by throwing inside of an async function without a
catch block, or by rejecting a promise which was not handled with .catch().
The promise rejected with the reason "That item is sold out.".] {
code: 'ERR_UNHANDLED_REJECTION'
}
This is the code:
const inventory = {
sunglasses: 0,
pants: 1088,
bags: 1344
};
// Write your code below:
const myExecutor = (resolve, reject) =>{
if(inventory.sunglasses > 0){
resolve('Sunglasses order processed.');
}else{
reject('That item is sold out.');
}
}
const orderSunglasses = () => {
return new Promise(myExecutor);
}
const orderPromise = orderSunglasses();
console.log(orderPromise);
I tried to find the error online and reading others Errors, but is not clear, but I have no idea, I am new in javascript.
答案1
得分: 1
const myExecutor = new Promise((resolve, reject) => {
if (inventory.sunglasses > 0) {
resolve('太阳镜订单已处理。');
} else {
reject('该物品已售罄。');
}
});
const orderSunglasses = () => {
return myExecutor;
}
const orderPromise = orderSunglasses();
orderPromise.then((value) => console.log(value)).catch((err) => console.log(err));
使用`then`和`catch`方法来检查您的Promise是否已被解决或拒绝,因为它可以处理两种类型的值。如果您不在`catch`块中检查错误,它将引发一个错误,因为您拒绝了一个未使用`.catch()`处理的Promise。
英文:
const myExecutor = new Promise((resolve, reject) =>{
if(inventory.sunglasses > 0){
resolve('Sunglasses order processed.');
}else{
reject('That item is sold out.');
}
});
const orderSunglasses = () => {
return myExecutor;
}
const orderPromise = orderSunglasses();
orderPromise.then((value)=>console.log(value)).catch((err)=>console.log(err));
use then and catch methods to check if your promise has been resolved or rejected, as it works with two types of values if you don't check for error in the catch block it will throw an error for rejecting a promise which was not handled with .catch()
答案2
得分: 1
以下是翻译好的部分:
"With typical promise consumption, we won’t know whether a promise will resolve or reject, so we’ll need to provide the logic for either case. We can pass both a success callback and a failure callback to .then()
."
const inventory = {
sunglasses: 0,
pants: 1088,
bags: 1344
};
// Write your code below:
let prom = new Promise((resolve, reject) => {
if (inventory.sunglasses > 0) {
resolve('Sunglasses order processed.');
} else {
reject('That item is sold out.');
}
});
const handleSuccess = (resolvedValue) => {
console.log(resolvedValue);
};
const handleFailure = (rejectionReason) => {
console.log(rejectionReason);
};
prom.then(handleSuccess, handleFailure);
英文:
With typical promise consumption, we won’t know whether a promise will resolve or reject, so we’ll need to provide the logic for either case. We can pass both a success callback and a failure callback to .then()
.
const inventory = {
sunglasses: 0,
pants: 1088,
bags: 1344
};
// Write your code below:
let prom = new Promise((resolve, reject) =>{
if(inventory.sunglasses > 0){
resolve('Sunglasses order processed.');
}else{
reject('That item is sold out.');
}
});
const handleSuccess = (resolvedValue) => {
console.log(resolvedValue);
};
const handleFailure = (rejectionReason) => {
console.log(rejectionReason);
};
prom.then(handleSuccess, handleFailure);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论