Hemanth's Scribes

javascript

Rethinking Async in JavaScript

Author Photo

Hemanth HM

Thumbnail

A dramatic dialogue about async evolution in JavaScript:

The Callback Era

function read(filename, callback) {
  fs.readFile(filename, 'utf8', function(err, res) {
    if (err) return callback(err);
    callback(null, res);
  });
}

Avoiding Callback Hell:

  • Name your functions
  • Keep code shallow
  • Modularize
  • Bind this

The Promise Era

promise.then(result => {
  console.log(result);
}).catch(err => {
  console.log(err);
});

## The Async/Await Era
javascript
async function getData() {
  const result = await fetch(url);
  return result.json();
}

From callbacks to Promises to async/await - the journey continues!

#javascript#async
Author Photo

About Hemanth HM

Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.