- Node.js Version: v9.11.1
- OS: windows 10
- Scope (install, code, runtime, meta, other?): ?
- Module (and version) (if relevant): ???
We have a code like this
'use strict';
var a = new Promise((res, rej) => {
rej(123);
console.log('not ok');
});
a.then(() => console.log(1));
a.catch(a => console.log(a));
The output is following:
> node example.js
not ok
123
(node:4748) UnhandledPromiseRejectionWarning: 123
(node:4748) 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:4748) [DEP0018] DeprecationWarning: Unhandled promise rejections are depre
cated. 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
We have a code like this
The output is following:
So we are confused about why this happens:
not okprinted before123?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