Using async/await in TypeScript / JavaScript

async and await are keywords commonly used on C#. They allow you to perform async operations without the callback hell.

The “callback hell” happens when you perform multiple async operations. For each operation you need to send a callback, a function that will execute when the operation completes.

hadouken-code

But finally async and await came to the rescue! It is on C# 4.0, ECMAScript 7 and also available on Python 3.5.

Those keywords allow you to flatten your code, but still run the operation asynchronously.

Something like this:

(function () {
  fs.readFile('DATA', 'utf8', function (err, contents) {
    if (err) {
      return console.error(err);
    }
    callContentApi(contents, function (err) {
      if (err) {
        return console.error(err);
      }
      console.log('Content saved');
    });
  });
}());

Can be written as:

(async function () {
  try {
    const contents = await fs.readFile('DATA', 'utf8');
    await callContentApi(contents);
    console.log('Content saved');
  }
  catch (err) {
    console.error(err);
  }
}());

Now it is linear, it is easier to understand the flow of the application.

Using a Promise you can create a wait function. You can use the keyword await on functions that are async or that return a new Promise.

function wait(ms) {
  return new Promise((resolve, reject) => {
    setTimeout(function () {
      resolve();
    }, ms);
  });
}

(async function () {
  await wait(1000);
  console.log('After a second!');
}());

See

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s