-
Notifications
You must be signed in to change notification settings - Fork 106
How to handle "unhandled Promise rejections" #72
Comments
If I'm reading it correctly, your example code desugars to something like the following: function getReadyForBed() {
let teethPromise = brushTeeth();
let tempPromise = getRoomTemperature();
// Change clothes based on room temperature
return Promise.resolve(tempPromise)
.then(temp => {
// Assume `changeClothes` also returns a Promise
if (temp > 20) {
return Promise.resolve(changeClothes("warm"));
} else {
return Promise.resolve(changeClothes("cold"));
}
})
.then(teethPromise)
.then(Promise.resolve()); // since the async function returns nothing, ensure it's a resolved promise for `undefined`, unless it's previously rejected
} When If I make a |
I think this concern is about promises not about async functions. Proposals for unhandled rejection tracking exist, eg here: https://github.com/domenic/unhandled-rejections-browser-spec/ (and a proposal to add the appropriate machinery to test262 is here: tc39/ecma262#76). |
@bterlson - Good point. Also, as one of my colleagues pointed out, my example could easily be re-written to avoid this pitfall: async function getReadyForBed() {
let teethPromise = brushTeeth();
let tempPromise = getRoomTemperature();
// Change clothes based on room temperature
var clothesPromise = tempPromise.then(function(temp) {
// Assume `changeClothes` also returns a Promise
if(temp > 20) {
return changeClothes("warm");
} else {
return changeClothes("cold");
}
});
/* Note that clothesPromise resolves to the result of `changeClothes`
due to Promise "chaining" magic. */
// Combine promises and await them both
await Promise.all(teethPromise, clothesPromise);
} Note that this should prevent any unhandled promise rejection. |
It certainly won't - if either The only way to prevent it is to always remember to add a |
@ljharb - What do you mean? EDIT: I meant that we don't care about the Promise returned by calling You are right that if |
@bminer all async functions return Promises. If they don't return anything, it returns |
Not sure if this thread is still active. I am getting a similar error and it's preventing our users on IE11 from accessing the site (they click the login button after typing their credentials but nothing happens). I looked at developer tools to see if there's any errors and found the following "Unhandled promise rejection error: access is denied" Any ideas? |
if we consider the following example async function one() {
await Promise.reject('err');
}
function two() {
try {
one();
} catch (e) {
console.log(e); // uncaught
}
} When I am trying to handle the error async function one() {
try {
await Promise.reject('err');
} catch (e) {
// e caught here
}
} Should it not behave as follows? function one() {
return Promise.reject('error');
}
function two() {
one().catch(e => {
// error caught
});
}
|
@madhusudhand - In both examples, function |
I've tried to fix this thing for hours today. The latest version I can use without getting this warning is 5.12. |
@gur111 I don't think you meant to comment here. |
True. I meant to comment on the github issue. Sorry |
@madhusudhand, the two() function should be also
|
with async await, you need to respect "await" promise chain to catch errors.. |
(node:26359) 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: 5) i have facing this error anybody sloved it please |
@NOMAN-JRK add |
dear sir; please tell me where and which file please tell me name of file🙏🙏 |
@NOMAN-JRK please ask on IRC, or stack overflow; this repo is for a completed language proposal. |
I have been facing and trying to resolve the unhandled promise rejection for weeks now. |
K |
Promises can be "handled" after they are rejected. That is, one can call a promise's
reject
callback before providing acatch
handler. This behavior is a little bothersome to me because one can write...... and in this case, the Promise is rejected silently. If one forgets to add a
catch
handler, code will continue to silently run without errors. This could lead to lingering and hard-to-find bugs.In the case of Node.js, there is talk of handling these unhandled Promise rejections and reporting the problems. This brings me to ES7 async/await. Consider this example:
In the example above, suppose
teethPromise
was rejected (Error: out of toothpaste!) beforegetRoomTemperature
was fulfilled. In this case, there would be an unhandled Promise rejection untilawait teethPromise
.My point is this... if we consider unhandled Promise rejections to be a problem, Promises that are later handled by an
await
might get inadvertently reported as bugs. Then again, if we consider unhandled Promise rejections to not be problematic, legitimate bugs might not get reported.Thoughts on this?
This is related to the discussion found in the Node.js project here: nodejs/node#830
The text was updated successfully, but these errors were encountered: