async vs sync promise code example

Example 1: async vs sync

synchronous (sync) - you can only execute one thing at a time
asynchronous (async) - you can execute multiple things at the same time

Example 2: async await promise all javascript

let characterResponse = await fetch('http://swapi.co/api/people/2/')
let characterResponseJson = await characterResponse.json()
let films = await Promise.all(
  characterResponseJson.films.map(async filmUrl => {
    let filmResponse = await fetch(filmUrl)
    return filmResponse.json()
  })
)
console.log(films)

Tags:

Misc Example