Thoughts on Promises

I’ve always found promises rather confusing. After recapping it for the nth time this morning, I would like to detail here my main takeaways so that I can finally commit them to memory. Recap on Promises Firstly, to understand promises, you first have to understand threading. Javascript is single-threaded, meaning that all code execute line by line, one after another. When you create a promise, you’re essentially creating another thread that is added to the event queue and importantly, executed after your main code block. ...

December 7, 2021 · 5 min · Lei

Notes on Error handling

The hallmark of an experienced developer is his/her ability to foresee the errors and handle them appropriately. Below are some notes regarding how error handling is done in different contexts. Let’s first take a look at the general structure of error handling. Structure of error handling Within a .then().catch() block For instance, within a route, you want to find the noteById, and then do something. 1 2 3 4 5 6 Note.findById(id) .then(note => note) .catch(error => { console.log(error) response.status(500).end() }) Often times, when you’re making a request to the database, it’s unclear whether your request will succeed. If there is an error, you’ll need a .catch() block to handle the error. The catch block receives a callback function with error as argument - an error handler function - and you’ll then define how to handle this error. ...

September 24, 2021 · 5 min · Lei