In this week’s blogpost, after seeing a piece of code in a previous assignment I wanted to learn a little bit more about what it actually was. The piece of code in JavaScript used async and await, and me being a novice in JavaScript didn’t exactly know what it was, but did a little bit contextually. At first glance they seem like timed functions to synchronize something. In exploring what these were I came across this article on error handling in JavaScript.
https://www.valentinog.com/blog/error/
This blog goes into great detail over error handling in JavaScript, and I’ve learned quite a bit more as well as answering the async and await question.
First off, I did not know that JavaScript was single threaded, meaning that it doesn’t utilize multi-threaded processes to handle certain bits of logic simultaneously. The article gives a good example with a timed error throw with an error catcher to demonstrate how the try/catch will already execute before the error would ever be called.
I was also introduced to some new interesting features of JavaScript like Promise. Promise is indicative of it’s name and is basically like a promise of the code to catch up with each other later. Promise uses “.then”, “.catch” and “.finally” where the only real difference is in name for “.then” as it’s the counterpart to “try”. It also has some more singular uses like “.all”, “.any” and “.race”. These are just different methods for finding an error. All will take an array of “promises” and find any of the errors from the promises and return the promises that resolved. Any will give you the first of the “promises” that resolved. Race is basically just a race to see who completes first regardless of error or not.
Asny and Await are two functions that you generally prefix to another function. Async forces the function to return a promise which allows us to use the Promise “.then”, “.catch” and “.finally” to resolve outside the function. But it doesn’t also mean that we can’t use try and catch as with “await” it allows us to literally wait for a function to resolve and with try and catch we can handle the errors inside of the current function.
Overall this article was very helpful. My intention was to just find an explanation as to what async and await meant, but I ended up with a little crash course on interesting ways of handling errors in JavaScript, as well as clear meanings behind each one.
From the blog CS@Worcester – A Boolean Not An Or by Julion DeVincentis and used with permission of the author. All other rights reserved by the author.