我的承诺链中的承诺被拒绝 "reject is not defined"
Promise rejected "reject is not defined" in my chain of Promises
我有以下承诺链:
let promise = new Promise((resolve, reject) => {
imgToDisplay.onload = () => {
resolve(imgToDisplay.width);
}
})
.then((result) => {
window.URL.revokeObjectURL(imgToDisplay.src);
if (result >= 20)
reject('Image width too large');
})
.then(() => {
//I do something with the image file here if it is not rejected
})
.catch((e) => {
alert(e.message); //This happened, however "reject is not defined"
});
我没有 reject
也没有 resolve
我在第二个 then()
中的承诺。我这里做错了什么?
除非您直接在 Promise 构造函数中,否则您不能调用reject
,因为它不在范围内。但是,如果您在 .then
中抛出错误,您可以强制控制流转到链中的下一个 .catch
(跳过中间的 .then
):
let promise = new Promise((resolve, reject) => {
imgToDisplay.onload = () => {
resolve(imgToDisplay.width);
}
})
.then((result) => {
window.URL.revokeObjectURL(imgToDisplay.src);
if (result >= 20)
// This will result in the Promise being rejected:
throw new Error('Image width too large');
})
.then(() => {
//I do something with the image file here if it is not rejected
})
.catch((e) => {
alert(e.message); //This happened, however "reject is not defined"
});
但是在这种情况下,由于导致被拒绝的 Promise 的测试可以在 Promise 构造函数中完成,您可以改为在构造函数中调用 reject
,如果你想要:
let promise = new Promise((resolve, reject) => {
imgToDisplay.onload = () => {
if (imgToDisplay.width >= 20) {
reject('Image width too large');
}
resolve(imgToDisplay.width);
}
})
在 then
回调中使用 throw
而不是拒绝
let promise = new Promise((resolve, reject) => {
imgToDisplay.onload = () => {
resolve(imgToDisplay.width);
}
})
.then((result) => {
window.URL.revokeObjectURL(imgToDisplay.src);
if (result >= 20)
throw 'Image width too large';
})
.then(() => {
//I do something with the image file here if it is not rejected
})
.catch((e) => {
alert(e.message); //This happened, however "reject is not defined"
});
我有以下承诺链:
let promise = new Promise((resolve, reject) => {
imgToDisplay.onload = () => {
resolve(imgToDisplay.width);
}
})
.then((result) => {
window.URL.revokeObjectURL(imgToDisplay.src);
if (result >= 20)
reject('Image width too large');
})
.then(() => {
//I do something with the image file here if it is not rejected
})
.catch((e) => {
alert(e.message); //This happened, however "reject is not defined"
});
我没有 reject
也没有 resolve
我在第二个 then()
中的承诺。我这里做错了什么?
除非您直接在 Promise 构造函数中,否则您不能调用reject
,因为它不在范围内。但是,如果您在 .then
中抛出错误,您可以强制控制流转到链中的下一个 .catch
(跳过中间的 .then
):
let promise = new Promise((resolve, reject) => {
imgToDisplay.onload = () => {
resolve(imgToDisplay.width);
}
})
.then((result) => {
window.URL.revokeObjectURL(imgToDisplay.src);
if (result >= 20)
// This will result in the Promise being rejected:
throw new Error('Image width too large');
})
.then(() => {
//I do something with the image file here if it is not rejected
})
.catch((e) => {
alert(e.message); //This happened, however "reject is not defined"
});
但是在这种情况下,由于导致被拒绝的 Promise 的测试可以在 Promise 构造函数中完成,您可以改为在构造函数中调用 reject
,如果你想要:
let promise = new Promise((resolve, reject) => {
imgToDisplay.onload = () => {
if (imgToDisplay.width >= 20) {
reject('Image width too large');
}
resolve(imgToDisplay.width);
}
})
在 then
回调中使用 throw
而不是拒绝
let promise = new Promise((resolve, reject) => {
imgToDisplay.onload = () => {
resolve(imgToDisplay.width);
}
})
.then((result) => {
window.URL.revokeObjectURL(imgToDisplay.src);
if (result >= 20)
throw 'Image width too large';
})
.then(() => {
//I do something with the image file here if it is not rejected
})
.catch((e) => {
alert(e.message); //This happened, however "reject is not defined"
});