admin管理员组文章数量:1278721
Their is already a question on this topic
Node.js Best Practice Exception Handling
which is old and answers are very much outdated, domains
have even deprecated since then.
Now in a post Async/Await Node.js scenario shouldn't we consider sync and async cases similarly and throw exceptions in sync functions and rejecting promises in async functions instead of returning an Error
instance in the former 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
}
}
Simulating async divide 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);
})
};
So sync and async exceptions can be handled in a uniform manner
let main = async function () {
try {
//const resultSync = divideSync(4,0);
const resultAsync = await divideAsync(4,0);
}
catch(ex) {
console.log(ex.message);
}
}
Their is already a question on this topic
Node.js Best Practice Exception Handling
which is old and answers are very much outdated, domains
have even deprecated since then.
Now in a post Async/Await Node.js scenario shouldn't we consider sync and async cases similarly and throw exceptions in sync functions and rejecting promises in async functions instead of returning an Error
instance in the former 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
}
}
Simulating async divide 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);
})
};
So sync and async exceptions can be handled in a uniform manner
let main = async function () {
try {
//const resultSync = divideSync(4,0);
const resultAsync = await divideAsync(4,0);
}
catch(ex) {
console.log(ex.message);
}
}
Share
Improve this question
edited May 23, 2017 at 12:10
CommunityBot
11 silver badge
asked Sep 23, 2016 at 7:26
adnan kamiliadnan kamili
9,4659 gold badges74 silver badges133 bronze badges
1 Answer
Reset to default 11The answers from Node.js Best Practice Exception Handling are old and very much outdated
Not that much. This answer, with the list from this well-maintained blog post, is quite up to date.
The offical node.js guide is always a good read, and the general approach hasn't changed that much.
So what has changed?
- Domains are broken and deprecated. Well, that's old news.
- The typical "node-style callbacks" with their error-first-parameter, which are fired exactly once, should no more be used. This simple sequential asynchronous coding style with all its problems has been superseeded by promises and
async
/await
. (Note: event emitters etc are a different case) process.on('uncaughtException')
is supplemented byprocess.on('unhandledRejection')
- promises also catch programmer errors if used correctly. For boring sequential asynchronous code, they can replace domains.
So what does this mean for the mon code?
Shouldn't we consider sync and async cases similarly and throw exceptions in sync functions and rejecting promises in async functions instead of returning an
Error
instance?
Yes, exactly. You should reject your promises with Error
s (or throw
them from async function
s).
Notice you will rarely have to call reject
yourself. With promises, you should be able to throw
in your code. If you can't, you're likely not using them correctly - and programmer mistakes wouldn't be caught either there.
The golden rule for this code is: Never use callbacks that are not promise callbacks. "Promise callbacks" refers to the new Promise
, then
, and catch
arguments, and possibly some of the custom methods of your library (e.g. finally
). This is where your example code above has a problem. Written correctly, it should 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;
}
}
本文标签: javascriptNodejs Best Practice Exception HandlingAfter AsyncAwaitStack Overflow
版权声明:本文标题:javascript - Node.js Best Practice Exception Handling - After AsyncAwait - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741235293a2362891.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论