I would like to write about a common mistake of new javascript or nodejs developers make. Especially, the type of developers who have a high level language like C# or JAVA background. As you might know these languages are called multi paradigm programming languages, where you can find some of functional programming language concepts as well as object oriented concepts.

But in general, the execution and writing order of these languages are same. Meaning the lines at the start executes first. Let me show you a C# code block and describe what I mean.

Simple Try Catch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private IList<TResult> GetDBList<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, TResult>> selector)
{
try
{
Expression<Func<U, bool>> convertedPredicate = ExpressionHelper.TypeConvert<T, U, bool, bool>(predicate);
IQueryable<U> queryable = CurrentDbSet.Where(convertedPredicate);
Expression<Func<U, TResult>> convertedSelector = ExpressionHelper.TypeConvert<T, U, TResult, TResult>(selector);
List<TResult> dbList = queryable.Select(convertedSelector).ToList();
return dbList;
}
catch (ex)
{
throw ex.GetBaseException();
}
}

So what is going on here :). An expression of one type converted to an expression of DB model types of entity framework and than a list of objects returned based on the expression. Also trys the block and catch the base exception of this. Anyway, this code block is not our main topic for this post, but what I would like to underline here is; there are numerious other methods called and here each one of them executes synchronously. If one of them fail to run and throws an exception, execution stops there and moves to catch block.

But, things are not like this in javascript world…

Let’s try to mimic this into javascript. Though I will not go to details but only mimic the lines of codes.

We don’t need this all type definitions in javascript, thank god :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Our main method returns the docs as the second parameter of the callback.
function getDBList(predicate, selector, callback) {
try {
// Synchronous calls.
var convertedPredicate = ExpressionHelper.TypeConvert(predicate);
var queryable = CurrentDBSet.Where(convertedPredicate);
var convertedSelector = ExpressionHelper.TypeConvert(selector);
// Asynchronous call
queryable.Select(convertedSelector, function(err, list){
// No error and list is returned to our caller.
return callback(null, list);
});
} catch(ex) {
throw ex;
}
}

Here we are trying to accomplish the same thing with a direct conversion of the code, and do what people told us about db access : “Database operations should not be blocking in Node.JS”

You Cannot Catch Me

But, there is a huge problem here, the execution order is not like C# developer’s expectation. Moreover, the catch statement here only catches the exceptions of sync calls and never catches the db calls exceptions.

What is going on here?
Simple, this is not C# :) The single threaded NodeJS executes every line and there is no error to create a callback for our queryable.Select method. This is the definition of callback not its execution. After the execution finished, nodejs emitter executes the callback of our db call.

Another thing happens when we try to catch the exception of GetDBList in a higher hierarchy. Because our GetDBList is as asynchronous as db call is. Now no exception catchable at the top;

1
2
3
4
5
6
7
8
9
10
11
12
try {
getDBList(nullPredicate, nullSelector, function(err, dbEntities) {
if(err) {
console.log(err);
} else {
console.log(dbEntities);
}
});
} catch (ex) {
// Our C# guy trying to understand what is going on :)
console.log(ex);
}

This is the common mistake of new guys in javascript world.

Do Not Throw Exceptions in Callbacks

In node.js world that is why we implement callbacks signatures as err, result format. So the correct implementation should be like below;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function getDBList(predicate, selector, callback) {
// Synchronous calls.
try {
var convertedPredicate = ExpressionHelper.TypeConvert(predicate);
var queryable = CurrentDBSet.Where(convertedPredicate);
var convertedSelector = ExpressionHelper.TypeConvert(selector);
} catch(ex) {
return callback(ex);
}
// Asynchronous call
queryable.Select(convertedSelector, function(err, list){
if(err) {
return callback(err);
}
// No error and list is returned to our caller.
return callback(null, list);
});
}

That is it folks. Thanks for reading. I hope you enjoyed.

PS: C# guys, don’t take this personal, I am not here to offend anyone. I am also developing with C# anyway and personally did this mistake multiple times :)