[英]NodeJS program is not exiting after throwing an error UnhandledPromiseRejection
I have a function written in JavaScript.我有一个用 JavaScript 编写的函数。 I'm trying to call a function which performs some work from my main function.我正在尝试调用一个函数,该函数从我的主函数中执行一些工作。 I have put a condition in the calling function to look for an employee id and if null then I'm returning the error and would like to exit gracefully from my main function.我在调用函数中放置了一个条件来查找员工 ID,如果为 null,那么我将返回错误并希望从我的主函数中优雅地退出。 But this thing is not happening.但是这件事并没有发生。 Program is keep on executing to next code and functions and there getting below error:-程序继续执行到下一个代码和函数,并且出现以下错误:-
{ "errorType": "Runtime.UnhandledPromiseRejection", "errorMessage": "Error: Employee number is mandatory", "reason": { "errorType": "Error", "errorMessage": "Employee number is mandatory", "stack": [ "Error: Employee number is mandatory", " at /var/task/index.js:64:23", " at Array.forEach ()", " at transformData (/var/task/index.js:58:22)", " at /var/task/index.js:207:43", " at processTicksAndRejections (internal/process/task_queues.js:97:5)", " at async Promise.all (index 0)", " at async Runtime.exports.lambdaHandler [as handler] (/var/task/index.js:258:5)" ] }, "promise": {}, "stack": [ "Runtime.UnhandledPromiseRejection: Error: Employee number is mandatory", " at process. (/var/runtime/index.js:35:15)", " at process.emit (events.js:315:20)", " at process.EventEmitter.emit (domain.js:482:12)", " at processPromiseRejections (internal/process/promises.js:209:33)", " at processTicksAndRejections (internal/process/task_queues.js:98:32)" ] } { "errorType": "Runtime.UnhandledPromiseRejection", "errorMessage": "Error: Employee number is required", "reason": { "errorType": "Error", "errorMessage": "Employee number is required", "stack ": [ "错误:员工编号是强制性的"," at /var/task/index.js:64:23"," at Array.forEach()"," at transformData (/var/task/index.js: 58:22)", " at /var/task/index.js:207:43", " at processTicksAndRejections (internal/process/task_queues.js:97:5)", " at async Promise.all (index 0) ", " at async Runtime.exports.lambdaHandler [as handler] (/var/task/index.js:258:5)" ] }, "promise": {}, "stack": [ "Runtime.UnhandledPromiseRejection: Error : 员工编号是强制性的", " at process. (/var/runtime/index.js:35:15)", " at process.emit (events.js:315:20)", " at process.EventEmitter.emit (domain.js:482:12)", " at processPromiseRejections (internal/process/promises.js:209:33)", " at processTicksAndRejections (internal/process/task_queues.js:98:32)" ] }
Main function code snippet:-主要功能代码片段:-
try{
const data = await transformData(list, mymap, tenantId);
if (data === null || data === undefined) {
throw Error(`Error in transforming the data`);
}
const syncDataResponse = await syncDetails(tenantId!, data); //Calling is going here
} catch (err) {
console.log(err);
}
Calling function:-调用函数:-
const transformData = async (list: any, mymap: Map<string, string>, tenantId: string) => {
const data = [];
try {
await list.forEach(async (obj: { [x: string]: any; employee_number?: any; }) => {
modifyKeys(obj, mymap);
if (obj && obj.employee_number) {
data.push({ employeeId: obj.employee_number, employeeDetails: obj });
} else {
throw Error(`Employee number is mandatory`);
}
});
} catch (err) {
console.log('Error while transforming the data::', err);
return err;
}
return data;
}
I was expecting program exit gracefully when I'm throwing the error in the transformData function when the employee id is null but in my main method, the program does executes the next function called syncDetails() and I'm getting this unhandledpromiserejection error.当员工 ID 为空时,当我在 transformData 函数中抛出错误时,我期待程序正常退出,但在我的主方法中,程序确实执行了名为 syncDetails() 的下一个函数,并且我收到了这个未处理的承诺拒绝错误。
Pleas suggest.请建议。 Cheers干杯
In transformData you are using list.foreach method where your promise is going into the loop and they are continuous throwing error.在 transformData 中,您正在使用 list.foreach 方法,其中您的承诺进入循环并且它们连续抛出错误。
Instead of using list.foreach, I suggest use Promise.All我建议使用 Promise.All 而不是使用 list.foreach
Convert your forEach
in transformData
to for ... of
or a traditional for loop
instead.将transformData
的forEach
transformData
为for ... of
或传统的for loop
。
Although forEach
is synchronous in nature, it does NOT recognize async
functions.虽然forEach
是同步的性质,它不承认async
功能。
Refer to these articles for more info -有关更多信息,请参阅这些文章 -
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.