Last active
December 8, 2018 03:06
-
-
Save amslezak/545e1a6013caa87e96e6b53c6cfad1e0 to your computer and use it in GitHub Desktop.
simple for-await-of snippet/example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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