TGIMBA NodeJS – Part 7

Git Hub Code

NOTE: The API Git repository got scrambled and I had to start over.  The link above is correct.

Well, I have decided that passing the response object through multiple call backs is going to be a little crazy. I had to add two return methods just for the getUserCount and processUser SQL Server calls/endpoints. They are both fairly simple and the code that resulted (in hind sight) is convoluted. It will turn into a spaghetti mess with more calls.

So, in reading multiple articles and talking with some of my fellow engineers, I have discovered the preferred method in JavaScript is something called a Promise. A promise (as I understand it), is a way to chain multiple asynchronous methods together so that once the call back comes back, it will resolve and you can do something with the result.

You can still do this on your own or by passing a response (or other object I supposed) into the asynchronous method where it can be used once the method resolves. The thing about promises is that the code is a lot cleaner. Promises are also (or so I hear) what is behind the await/async functionality coming in the next major version of Javascript. Their use (I believe) is very similar to the await/async functionality used in .NET.

I found an excellent tutorial on the ‘basics’ of promises (see reference #1). Following this, I recreated its author’s code and was able to get a very small chain of calls in a simple API method. Very nice!

I then started walking down the path of adopting my existing code using the Tedious library, but I had some issues on the .on() event handlers. Every time I would reference the response object, it would be undefined. I suspect that these .on() event handlers need to be promises themselves. But in reading further, I discovered that someone had already made a Promise friendly version of the Tedious library (see reference #2). Following the instructions, I was very easily and quickly able to modify my TGIMBA Node JS API to use it.

Next post will be (ideally) with all of the other SQL methods implemented.

Stay tuned!



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s