Skip to content

Instantly share code, notes, and snippets.

@AlonsoK28
Last active July 7, 2023 06:40
Show Gist options
  • Save AlonsoK28/d48e7ba6763fccb26569d2cb91f11511 to your computer and use it in GitHub Desktop.
Save AlonsoK28/d48e7ba6763fccb26569d2cb91f11511 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));
@RichardBray
Copy link

Don't know why this doesn't have more comments. I found this really helpful 👍

@AlonsoK28
Copy link
Author

Don't know why this doesn't have more comments. I found this really helpful 👍

Thanks for your support.

Please share. 😎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment