Prologue
Sherlock Holmes, consultant detective is a character created by Sir Arthur Conan Doyle. He has a knack for solving cases using his science of deduction but if we observe there is a pattern that he follows. He starts his hunch with one clue, he follows that until it leads to another clue or if not he rejects it.
For example, when Sherlock meets Dr Watson for the first time, after a couple of minutes of observation, Sherlock asks Watson ‘Afghanistan or Iraq?’ What he meant was, were you posted to Afghanistan or Iraq as a military veteran? How did he conclude that, without knowing Watson before?
Watson’s Appearance - Sherlock observes various details about Watson's appearance, such as his haircut, which is military-style, and the way he carries himself. These details suggest a recent association with the military.
Watson’s Tan - Sherlock notices that Watson has a particular tan line on his left hand, indicating that he used to wear a military watch but has stopped recently. This observation further supports the idea that Watson is a recent military veteran.
Psychological Stress - Sherlock observes signs of psychological stress in Watson, indicating that he has experienced trauma or hardship. This aligns with the assumption that Watson has recently returned from a war zone.
Elimination of other war zones - Sherlock considers global geopolitical events and eliminates other possible war zones, narrowing it down to Afghanistan or Iraq as the most likely locations for Watson's military service.
If we observe the pattern here, we can understand that Sherlock uses the result of his previous observation while he investigates the next clue. Something like this
→ Analyze the first clue and gather more information → Pass the result
→ Incorporate the previous result with new clues → Pass the new result
→ Connect the dots and unveil a deeper layer of the mystery → Pass the result
→ Sherlock's deduction - the grand revelation
Promise Chaining
Promise chaining is a similar approach to what Sherlock uses to solve cases. This is how Sherlock would’ve written the above deduction using the JavaScript function.
The result
For easy use, I’ve templated the code, you can run and play with the above code here: Run Code
Why Promise Chaining
Promises are underrated and less utilized. But if an engineer uses it properly, there are amazing advantages.
Sequential Execution of Asynchronous Operations - Chaining allows you to execute asynchronous operations one after the other in a specific order. It also allows you to chain multiple asynchronous operations in sequential order.
Readability and Maintainability - It eliminates the need to write big functions and allows us to write small readable and reusable functions. Also, it’s like storytelling (step-wise), which is so easily readable.
Error Handling - Promises have built-in error propagation. If an error occurs at any point in the chain, it will automatically jump to the nearest catch block, making it easier to handle and log errors.
Apart from this, there are a few more advantages, I have jotted down all the advantages and 11 practical use cases with code examples where promise chaining is a perfect fit. As writing them down here is difficult, I’ve created a free PDF.
If you are already a Follywood subscriber then reply to this post from your inbox to receive the free book.
If you aren’t a subscriber, then subscribe and you’ll receive a mail, reply to that mail to receive the free book.
Or just leave a comment here to receive the free book.
Challenge
From your codebase try refactoring a less readable big function to clean smaller functions using Promises. And realize the magic.
Made with ♡
if Nayan fails to reply, exception handled by "you Know :)"
RequestForBook.then(()=> receiveBook()).catch(noBookReceivedError => callNayan())