How To Handle Errors in Asynchronous Javascript Code (when working with Callbacks)

Sherlynn Tan
3 min readAug 18, 2019

We will use a simple function called calculateSquare to demonstrate how to handle errors in asynchronous Javascript code.

In order to simulate an asynchronous function, we will use setTimeout:

function calculateSquare(number, callback) {  
setTimeout(() => {
const result = number * number…
Sherlynn Tan

Senior Software Developer at @thoughtworks | Marathon Runner | Ex-Pharmacist Writing on Coding, Technology and Running