Skip to content

Instantly share code, notes, and snippets.

@CypherpunkSamurai
Forked from AlonsoK28/fecth-api-iterate.js
Created November 6, 2022 09:30
Show Gist options
  • Save CypherpunkSamurai/456f13c49ebc6c3df09ed4c58600b466 to your computer and use it in GitHub Desktop.
Save CypherpunkSamurai/456f13c49ebc6c3df09ed4c58600b466 to your computer and use it in GitHub Desktop.
Iterate over Fetch API
const myData = [
{id:1, name:'data1'}, {id: 2, name: 'data2'}, {id:3, name: 'data3'}
];
async function mapJSFunc(currentValue) {
const url = 'https://api.github.com/?callback=foo'
return fetch(url)
.then(data => {
if (data.status === 200) {
return data;
}
})
.then(data => data.json())
.then(data => {
const newUrl = data.current_user_url;
return { ...currentValue, c: `I'am a prop with date: ${new Date()}`, newUrl };
})
}
let res = Promise.all(myData.map(mapJSFunc)).then(data => console.log(data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment