Skip to content

Instantly share code, notes, and snippets.

@amslezak
Last active December 8, 2018 03:06
Show Gist options
  • Save amslezak/545e1a6013caa87e96e6b53c6cfad1e0 to your computer and use it in GitHub Desktop.
Save amslezak/545e1a6013caa87e96e6b53c6cfad1e0 to your computer and use it in GitHub Desktop.
simple for-await-of snippet/example
// for await of
const fetch = require('node-fetch')
const urls = [
'https://swapi.co/api/people/1',
'https://swapi.co/api/people/2',
'https://swapi.co/api/people/3',
'https://swapi.co/api/people/4',
]
// sync
const loopThroughUrls = url => {
for (url of urls) {
console.log(url)
}
}
// async
const getData = async function() {
const arrayOfPromises = urls.map(url => fetch(url))
for await (let request of arrayOfPromises) {
const data = await request.json()
console.log(data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment