I have a bunch of code. The code has async function, that is promise. I'm awaiting my promise inside try...catch. If there is an error, I reject it with real promise. See this,
runService(options.target, options, socket)
.then(async id => {
if(user) {
.
.
.
})
.catch(error => {
console.log('Here', error);
return socket.emit('error', error);
});
The runService functions like this,
const startService = (target, options, socket) => {
return new Promise(async (resolve, reject) => {
target = typeof(target) == 'string' && target.trim() != '' ? target.trim() : false;
if(target) {
try {
let addresses = await dns.promises.lookup(url.parse(target).hostname, 4);
} catch(exception) {
return reject(exception);
}
const id = await createHash(32);
const targetSlug = url.parse(target).hostname.split('www.').reverse()[0].replace(/[-.]/g, '');
const date = new Date();
socket.emit('log', { stage: 1, message: 'Starting simulation and analysis process' });
const chrome = await launchChrome([
`--window-size=${options.fullscan ? 1920 : options.desktopResolution.width},${options.fullscan ? 1080 : options.desktopResolution.height}`,
'--disable-background-networking',
'--disable-sync',
'--disable-default-apps',
'--no-first-run',
'--enable-automation',
'--disable-translate',
'--disable-extensions',
'--mute-audio',
'--no-sandbox',
headless ? '--headless' : ''
]);
.
.
.
});
};
I'm using the try...catch and i call the function for it will exactly throws an exceptions in the,
let addresses = await dns.promises.lookup(url.parse(target).hostname, 4);
It throws and UnhandledPromiseRejectionWarning
and the output is this;
Output http://prntscr.com/mf60hr
Why there is an UnhandledPromiseRejectionWarning
and it doesn't call the socket.emit('error', error)
line on .catch()
block. Why is this?