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.
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”
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;
In node.js world that is why we implement callbacks signatures as err, result format. So the correct implementation should be like below;
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 :)