You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Promise - unhandled rejection warning has been thrown, although the rejection seems to be handled #1208
> node example.jsnot ok123(node:4748) UnhandledPromiseRejectionWarning: 123(node:4748) UnhandledPromiseRejectionWarning: Unhandled promise rejection. Thiserror originated either by throwing inside of an async function without a catchblock, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)(node:4748) [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.
So we are confused about why this happens:
Why is not ok printed before 123?
Why do we get unhandled warning while the rejection is handled (it has catch properly attached)?
How to do it properly (to avoid warning and to reject silently)?
Sorry if this question is noobish, can't figure out how to do this for a week. Tried stack overflow, but got no response and no votes at all, the post is ignored completely
@user8322 no question is noobish, and if it's troubling you then you should ask out without thinking too much about it :)
Regarding 2 and 3, the unhandled warning came up because it was, in fact, not handled properly, because you tried handling success and failure at different statements. The correct code would be:
'use strict';vara=newPromise((res,rej)=>{rej(123);console.log('not ok');});a.then(()=>console.log(1)).catch(a=>console.log(a));// or just .then(...).catch(...) on the same line, but this way it's more readable
and indeed, this work. See for yourself.
Regarding 1, this happens because of how Node.js works and it's internals (as well as v8's). To get started with promises (regarding syntax and internals), try out nodeschool's workshop, "Promise it won't hurt" at https://github.com/stevekane/promise-it-wont-hurt
Activity
ryzokuken commentedon Apr 11, 2018
@user8322 no question is noobish, and if it's troubling you then you should ask out without thinking too much about it :)
Regarding 2 and 3, the unhandled warning came up because it was, in fact, not handled properly, because you tried handling success and failure at different statements. The correct code would be:
and indeed, this work. See for yourself.
Regarding 1, this happens because of how Node.js works and it's internals (as well as v8's). To get started with promises (regarding syntax and internals), try out nodeschool's workshop, "Promise it won't hurt" at https://github.com/stevekane/promise-it-wont-hurt