I have an application where I continuously have to run some async code in the background. I created a minimal simulation of my application.
let promise_chain = Promise.resolve();
let rejected_promise_count = 0;
const interval_id = setInterval(
// Do some important polling. I will just always reject to demonstrate the problem.
() => {
promise_chain = promise_chain.then(() => {
rejected_promise_count += 1;
return Promise.reject();
})
},
10
);
// Set timeout simulates the program being done.
setTimeout(
() => {
clearInterval(interval_id);
promise_chain
.then(() => end("Resolved! :D"))
.catch(() => end("Rejected! D:"));
},
1000
);
function end(message) {
console.log(message);
console.log(`Amount of rejected promises created: `, rejected_promise_count);
}
This gives a long list of these:
(node:29217) UnhandledPromiseRejectionWarning: undefined
(node:29217) 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: 2)
(node:29217) [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.
(node:29217) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 2)
(node:29217) UnhandledPromiseRejectionWarning: undefined
(node:29217) 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: 3)
(node:29217) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 3)
(node:29217) UnhandledPromiseRejectionWarning: undefined
(node:29217) 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: 4)
(node:29217) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 4)
Eventually ended by these:
(node:29217) UnhandledPromiseRejectionWarning: undefined
(node:29217) 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: 87)
(node:29217) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 87)
(node:29217) UnhandledPromiseRejectionWarning: undefined
(node:29217) 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: 88)
Rejected! D:
Amount of rejected promises created: 1
(node:30920) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 89)
I'm not quite sure why node doesn't want me to handle these asynchronously, but given it runs on an interval, I really have no other choice.
How do I get rid of the endless list of warnings, and more importantly, how do I make sure in the future node will not terminate this process because it thinks I am not handling the rejections?
It depends what you want to do, the correct way to handle it is like this:
const interval_id = setInterval(
() => {
promise_chain = promise_chain.then(() => {
rejected_promise_count += 1;
return Promise.reject();
});
//ignore error here, you catch it in the setTimeout
promise_chain.catch(ignore=>ignore);
},
10
);
That will output:
Rejected! D:
Amount of rejected promises created: 1
This because you reject the first time so the chain is broken and all other then are not executed.
If you would like to continue executing and want to know how many passed and failed you can do something like this:
//using actual results
let results = [];
//special Fail value to indicate rejected promise
let Fail = function(reason){this.reason=reason;};
let isFail = object=>(object&&object.constructor===Fail);
let isNotFail = object=>!isFail(object);
let promise_chain;
let somePromise = ()=>Promise.reject(new Error("Some reason"));
const interval_id = setInterval(
() => {
promise_chain = (promise_chain||somePromise())
.then(
result => {
results.push(result);
return somePromise();
}
)
.catch(//catch the rejection and return a Fail type value
error=>new Fail(error)
);
},
10
);
// Set timeout simulates the program being done.
setTimeout(
() => {
clearInterval(interval_id);
promise_chain
.then(
result => {
//add the last result to results
results.push(result);
console.log(
"rejected promises:",
results.filter(isFail).length
//you can get the errors like so:
//results.filter(isFail).map(fail=>fail.reason)
);
console.log(
"resolved promises:",
results.filter(isNotFail).length
//results.filter(isNotFail) is an array of resolved values
)
}
);
//this will never reject because we catch rejected promises
// and add fail types to results
// .catch(() => end("Rejected! D:"));
},
1000
);
Related
currently I'm struggling with fingerprint in my previous project i solved this in this way:
const test = async (req, res, next) => {
// other code
const FingerprintJS = require('#fingerprintjs/fingerprintjs');
const store = require('store')
FingerprintJS.get({
preprocessor: function(key, value) {
return value
}
},function(components){
let values = components.map(function (component) { return component.value });
let device_id = FingerprintJS.x64hash128(values.join(''), 31);
store.set('print', { device: device_id })
});
await new Promise(resolve => setTimeout(resolve, 1000));
// other code
};
But now I cant set it up to work.. I'm getting error in console.log :
(node:4940) UnhandledPromiseRejectionWarning: TypeError: FingerprintJS.get is not a function
at login (C:\Users\PC06\Desktop\project\backend\controllers\clients-controllers.js:53:17)
(node:4940) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:4940) [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.
Any help for this?
This question already has answers here:
Waiting for more than one concurrent await operation
(4 answers)
Closed 3 years ago.
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
await is the javascript construct which converts the rejection of the promise to an exception.
That is, await is the construct which handles the rejection.
When you write:
try {
pingTask = ping('8.8.8.8');
} catch (e) {
console.log(e);
}
There's no await there, so there's nothing to convert the rejection to an exception, or indeed to handle the rejection in any way.
If you're going to call ping() without waiting, then you need a more explicit rejection handling.
EDIT
Here's a minimal reproducer:
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const test = (ms, msg) => {
return new Promise((resolve, reject) => {
sleep(ms).then(reject(msg)).catch(console.log);
});
};
(async () => {
let task1;
try {
const ms = 50;
task1 = test(ms, "hey");
await sleep(ms * 2); // otherwise you don't get an error
await task1;
} catch (e) {
console.log(e);
}
})().then(console.log).catch(console.log);
(node:12822) UnhandledPromiseRejectionWarning: hey (node:12822)
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:12822) [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. hey undefined (node:12822)
PromiseRejectionHandledWarning: Promise rejection was handled
asynchronously (rejection id: 1)
The Promise() constructor starts running test(), after 50 ms the promise is rejected but there is nothing to convert the rejection to an exception.
If we remove the 100 ms sleep, then await registers its "convert rejection to exception" logic onto the promise way before the 50 ms sleep is done, and so when the promise gets rejected ~49 ms after await was called, there's a handler to convert it to an exception.
I'm using a 3rd party module that wraps around their API. I have the following code:
const api = require('3rdpartyapi');
async function callAPI(params) {
try {
let result = await api.call(params);
return result;
}
catch(err) {
throw err; //will handle in other function
}
}
async function doSomething() {
try {
//...do stuff
let result = await callAPI({a:2,b:7});
console.log(result);
}
catch(err) {
console.error('oh no!', err);
}
}
Despite both try-catch blocks, the 3rd party API, when it losses connection to homebase (happens quite frequently :( ), blows up with:
(node:13128) UnhandledPromiseRejectionWarning: FetchError: request to https://www.example.com failed, reason: getaddrinfo ENOTFOUND
Followed by:
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)
How come none of my try-catch catch this? What exactly is unhandled, and how to actually handle this?
The point is the await only converts "first layer" rejection into error, however the promise can have promise inside, and their library may fail to catch the rejection inside. I have made a proof of concept 3rdpartyapi which can trigger the behavior you see:
(async function () {
// mock 3rdpartyapi
var api = {
call: async function(){
await new Promise((resolve, reject) => {
// why wrap a promise here? but i don't know
new Promise((innerResolve, innerReject) => {
innerReject('very sad'); // unfortunately this inner promise fail
reject('this sadness can bubble up');
})
})
}
};
// your original code
async function callAPI(params) {
try {
let result = await api.call(params);
return result;
} catch (err) {
throw err; //will handle in other function
}
}
async function doSomething() {
try {
//...do stuff
let result = await callAPI({
a: 2,
b: 7
});
console.log(result);
} catch (err) {
console.error('oh no!', err);
}
}
doSomething();
})();
Output:
$ node start.js
oh no! this sadness can bubble up
(node:17688) UnhandledPromiseRejectionWarning: very sad
(node:17688) 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:17688) [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.
In the following code obj1 and obj2 are arrays.
if(obj2.length != 0) {
func(obj1, obj2);
}
//execute func only when obj2 is not empty
var func = (obj1, obj2) => {
const Channel = obj2.reduce((acc, curVal) => {
obj1.forEach((item) => {
if (curVal.Channel.includes('Name')) {
item.dataarr.push({ 'MobileNo': curVal.mobnum})
}
})
return obj1;
}, [])
};
But when obj2 is empty I get this error :
(node:10837) UnhandledPromiseRejectionWarning: TypeError: obj2.reduce is not a function
at func (/home/user/Desktop/serv.js:708:37)
at breakdata (/home/user/Desktop/serv.js:720:5)
at cached.getMulti.then (/home/user/Desktop/serv.js:643:7)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:10837) 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:10837) [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.
Ignore these line from the error above :
at breakdata (/home/user/Desktop/serv.js:720:5)
at cached.getMulti.then (/home/user/Desktop/serv.js:643:7)\
as they are thrown because obj2.reduce doesn't give any output when the exception is thrown.
How do I handle : obj2.reduce is not a function when obj2 array is empty? I don't want this code to throw exceptions as the log file generated is huge.
I'm not getting how to add a catch block here. How do I do this?
I'm getting UnhandledPromiseRejectionWarning when I run this simple code:
var d = new Promise((resolve, reject) => {
if (false) {
resolve('hello world');
} else {
reject('no bueno');
}
});
d.then((data) => console.log('success : ', data));
d.catch((error) => console.error('error : ', error));
The complete response is:
error : no bueno
(node:12883) UnhandledPromiseRejectionWarning: no bueno
(node:12883) 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: 2)
(node:12883) [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.
Seems like d.catch() is being fired. I noticed that if comment out d.then(), the warning message disappears.
I'm calling the script from the terminal like node foobar.js.
Am I doing something wrong?
Tested with node v8.14, v10 and v11 under MacOS High Sierra.
d.then() creates a new promise that is rejected because d is rejected. That is the rejected promise that is not handled correctly.
You should chain .then and .catch instead:
d
.then((data) => console.log('success : ', data))
.catch((error) => console.error('error : ', error));