When the below code errors (ping()
rejects its promise), I get the warning. The HTTP function seems to error out just fine. Something must be happening in ping()
itself, I guess, which is somehow avoiding the try-catch.
Could someone enlighten me? (This is after a few attempts at changing things to get it working.)
(async () => {
try {
let webTask, pingTask;
try {
webTask = httpsGet(urls[0]);
} catch (e) {
console.log(e);
}
try {
pingTask = ping('8.8.8.8');
} catch (e) {
console.log(e);
}
try {
const webResult = await webTask;
console.log('HTTP result:', webResult);
} catch (e) {
console.log(e);
}
try {
const pingResult = await pingTask;
console.log('Ping result:', pingResult);
} catch (e) {
console.log(e);
}
} catch (e) {
console.log('Error:', e);
}
})();
The error is:
"main.js" 137 lines, 2945 characters
(node:58299) UnhandledPromiseRejectionWarning: #<Object>
(node:58299) 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(). (rejection id: 1)
(node:58299) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
This is earlier in the file where I define my ping
function:
const netping = require('net-ping');
const pingSession = netping.createSession({
retries: 0,
timeout: 10000
});
const ping = ip => {
return new Promise((resolve, reject) => {
let result = { ipAddress: ip, start: new Date(Date.now()), end: null, error: null, duration_ms: -1 };
pingSession.pingHost(ip, (error, target) => {
result.end = new Date(Date.now());
result.duration_ms = result.end - result.start;
if (error) {
result.error = error;
console.log('rejecting promise');
reject(result);
} else {
resolve(result);
console.log('resolving promise');
}
});
});
};
NodeJS 11.13.0