Friday 24 March 2017

Async/await, exceptions and logic question

Hello, I would like to start a discussion regarding handling of errors/exceptions in async/await code.When we were using callbacks, we used to write code that with standard node convention of callback(err, result). So we used err to signal that something didnt go as expected, meaning we could handle logic through err. For example, function called getUser(id, callback) could return an error in callback saying that user wasn't found, or that the DB is not available, or something else. Now, naturally, the (err, result) style callbacks moved to promises, and err became rejection cause. This was natural. But now we are here with async/await in node 7.6.0 and the rejection cause is now exception thrown. Now this is weird, because the logic flow moved from 1st argument of callback to exception. This is actually discouraged in many languages, to use exceptions to control non exceptional flow (providing wrong user name is not that exceptional at all). for example lets have an async function authenticateUser(login,password) which may return an user or may throw(reject with) many different errors like user not found, wrong password, email not confirmed etc. Now all these errors become part of the API of this function.This doesn't feel right.How do you feel about it? Should the errors be returned in form of a pair of values instead, like they do in Go?

Submitted March 24, 2017 at 03:24PM by newreddit0r

No comments:

Post a Comment