在使用Promise时,如果出现了未处理的拒绝(rejected)状态,AWS Lambda Nodejs会抛出“Unhandled Promise Rejection”错误。要避免这种错误,可以使用try-catch语句或者.catch()函数来捕获reject状态并进行处理。
以下是使用try-catch语句的示例代码:
exports.myHandler = async (event, context) => { try { const result = await myPromiseFunction(); console.log(result); } catch (err) { console.error(err); } };
以下是使用.catch()函数的示例代码:
exports.myHandler = async (event, context) => { myPromiseFunction() .then(result => { console.log(result); }) .catch(err => { console.error(err); }); };