I am using async
function with IIFE.
But when I run this code by rest api call,
it says
UnhandledPromiseRejectionWarning: Unhandled promise rejection. 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().
I made try
and catch
inside of 'for loop'.
How can I make the correct try catch in this case?
(async () => {
for (let i = 0; i < keywordResult.places.length; i += 1) {
try {
const transformKeywordResult = await rp({
method: 'GET',
uri:
'',
qs: {
query: query
},
json: true
});
let addrInfo = transformKeywordResult.addresses[i];
if (!addrInfo.addressElements[8].longName) {
addrInfo.addressElements[8].longName = '';
}
searchResult.push(transformKeywordResult);
} catch (error) {
return res.status(200).json({
success: false,
code: 500,
msg: 'Internal Server Error',
err: error,
pos: 0
});
}
}
return res.send(searchResult);
})();
await
should be caught properly – CertainPerformance