Async/Await—Simple Example in Four Lines of Code

Home / Tips / Async/Await—Simple Example in Four Lines of Code
Async/Await—Simple Example in Four Lines of Code

Understanding ES6′ Async/Await

The simple example:

(async function() {
  let a = await fetch('/')
  console.log(await a.text())
})()

The simple example with a simple explanation:

The simple example again, this time with a in-depth explanation :

There’s much more about async/await but this might be a good start. Even if you don’t know anything about Promises yet, read the example, put the four lines into the browser’s JS console and play around: Leave one or both ‘awaits’ away, read the error messages and you should slowly get a feeling.

Source: https://medium.com/@theden/async-await-simple-example-in-four-lines-of-code-b3bc02c2fa34