Javascript Promises if I had to oversimplify are to my understanding a "way to act upon stuff later, scheduled via the .then()
method".
After doing the following in my terminal:
BASE$> node
> var promise = Promise.reject("reason 42");
I was therefore supprised to see this result:
> (node:8783) UnhandledPromiseRejectionWarning: reason 42
(node:8783) 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:8783) [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.
because I was about to write.
> promise.catch(console.log);
The thing that motiviates this question is:
Can I be reasonably sure that node.js
only pulls of this warnings (and threat "in the future I will bail completely", because of the code being run stepwise in the node.js console/REPL?
How has node.js
come already to the conclusion that the promise rejection was to be unhandled?
I have hence tested the following to work differently
- Evaluation of "same code" combined in one REPL iteration:
var promise = Promise.reject("reason 42"); promise.catch(console.log);
- Evaluation of "same code" from a file (e.g.
tmp.js
) with contentvar promise = Promise.reject("reason 42") promise.catch(console.log);`)
vianode tmp.js
both yielding the expected output "reason 42
" not presenting any warning as shown above earlier.
Hence how does this work out? Can my assumption be confirmed that the determination of an unhandled promise in the node console REPL
, is reflection the reach end in each REPL
loop iteration?