ES8 - Async Functions

This tutorial covers ES8 Async Functions tutorials, examples introduced to latest javascript features, in ES8, ES2017, with examples.

javascript Async Functions

Async functions are introduced to simplify the promised based operations. Async keyword introduce to simply asynchronous operations.

Let’s see an example before and after ES8 to javascript features.

axios.get('/api/employes')
.then((response) => {
  console.log(response);
}, (error) => {
  console.log(error);
});

The same can be rewritten using Async function, asyc functions are returns promises, so

async getEmployes(url) => {
  try {
    const response = await axios.get('/api/employes');
    console.log(response);
    return response;
  } catch (error) {
    console.log(error)
  }
}
getEmployes(url).then(emps => {
    console.log(emps);
});

These makes easier to read the code looks like synchronous code, Await is not compulsory

Support This will support in latest browsers To support in older browsers, enable the following plugins in your project - polyfill - babel plugin

Prev