如何故意使承诺失败?有时我只是跳过测试并假设一切正常,但我想故意做出失败的承诺,以便我的捕获正常工作。
exports.submitJob = async (req, res, next) => {
const { cv } = req.body
const userId = req.user._id
try {
if(!cv) {
//how to pass error to catch block?
}
const save_submission = new Submission({
userId,
cv
}).save()
} catch(e => {
res.json({
status: 0,
error: e
})
})
next()
}
你可以throw new Error('<your string here>');
:
请注意,这catch
不是与function
语法一起使用的东西- 正确的语法是catch (e) { /* block that uses e */ }
const submitJobWhichWillFail = async (req, res, next) => {
const cv = null;
try {
if (!cv) {
throw new Error('cv must not be falsey!');
}
const save_submission = new Submission({
userId,
cv
}).save()
} catch (e) {
console.log('res.json with error ' + e);
}
}
submitJobWhichWillFail();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句