javascript Async Functions

Async functions make to simplify the promised-based operations and simplify 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);
});

This makes it easier to read the code looks like synchronous code, Await is not compulsory.

Support It will support in latest browsers To support in older browsers, enable the following plugins in your project

  • polyfill
  • babel plugin