Skip to content

Instantly share code, notes, and snippets.

@SastraNababan
Created October 30, 2017 19:42
Show Gist options
  • Save SastraNababan/fa78c3c54604ee103f37a04b810e062f to your computer and use it in GitHub Desktop.
Save SastraNababan/fa78c3c54604ee103f37a04b810e062f to your computer and use it in GitHub Desktop.
Promise in Looping Example
const doFetch = (url) => fetch(url).then(result => result.json())
let urls =[
'https://jsonplaceholder.typicode.com/posts/1',
'https://jsonplaceholder.typicode.com/posts/2',
'https://jsonplaceholder.typicode.com/posts/3',
'https://jsonplaceholder.typicode.com/posts/4',
]
let promises=[]
urls.map((url) => {
promises.push(doFetch(url))
})
Promise.all(promises)
.then(results => console.log(results))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment