Node.js Best Practice Exception Handling - After Async / Await

Their question is already on this topic.

Node.js Best Practice Exception Handling

which is old and the answers are very outdated, domains haven't even been outdated since.

Now, in the Async / Await Node.js script, you should not consider similar cases synchronously and asynchronously, as well as exclude exceptions from synchronization functions and reject promises in asynchronous functions instead of returning the Error instance to the previous case.

 let divideSync = function(x,y) { // if error condition? if ( y === 0 ) { // "throw" the error throw new Error("Can't divide by zero exception") } else { // no error occured, continue on return x/y } } 

Simulate async deactivation operation

 let divideAsync = function(x, y) { return new Promise(function(resolve, reject) { setTimeout(function() { // if error condition? if (y === 0) { // "throw" the error safely by rejecting the promise reject (new Error("Can't divide by zero exception")); } else { // no error occured, continue on resolve(x / y) } }, 1000); }) }; 

Thus, synchronous and asynchronous exceptions can be handled uniformly.

 let main = async function () { try { //const resultSync = divideSync(4,0); const resultAsync = await divideAsync(4,0); } catch(ex) { console.log(ex.message); } } 
+6
source share
1 answer

Answers Node.js Practice exceptions to practice are outdated and very outdated

Not so much. This answer , with a list of this well-maintained blog post , is quite up to date.
The official node.js manual is always well read, and the general approach has not changed much.

So what has changed?

  • Domains are broken and outdated. Well, this is old news.
  • Typical "node callbacks" with their error-first parameter that run exactly once should no longer be used. This simple consistent asynchronous coding style with all its problems has been replaced by promises and async / await . (Note: event emitters, etc. are another case).
  • process.on('uncaughtException') is complemented by process.on('unhandledRejection')
  • promises also catch programmer errors when used correctly. To drill serial asynchronous code, they can replace domains.

So what does this mean for generic code?

Should we not consider synchronous and asynchronous searches in a similar way and exclude exceptions from synchronization functions and reject promises in asynchronous functions instead of returning an Error instance?

Yes exactly. You must reject your promises with Error (or throw them from async function s).

Note that you rarely have to call reject yourself. With promises, you should be able to throw in your code. If you cannot, you probably are not using them correctly - and the programmer’s errors will not be caught either there.

The golden rule for this code is: Never use callbacks that are not promises. "Promise callbacks" refers to the new Promise , then and catch arguments, and possibly some of your library’s custom methods (for example, finally ). Here is an example code example above. Correctly written, it must read

 async function divideAsync(x, y) { await new Promise(resolve => setTimeout(resolve, 1000) // don't even pass a function expression ); if (y === 0) { throw new Error("Can't divide by zero exception"); } else { return x / y; } } 
+8
source

All Articles