Answer the question
In order to leave comments, you need to log in
How to return json and url from Promise.all?
Code example: https://jsfiddle.net/kza92th7/
const fetchData = () => {
const 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",
"https://jsonplaceholder.typicode.com/posts/5",
"https://jsonplaceholder.typicode.com/posts/6",
"https://jsonplaceholder.typicode.com/posts/7",
"https://jsonplaceholder.typicode.com/posts/8"
];
const allRequests = urls.map(url =>
fetch(url).then(response => response.json())
);
return Promise.all(allRequests);
};
fetchData().then(r => console.log(r));
Answer the question
In order to leave comments, you need to log in
const fetchData = () => {
const 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",
"https://jsonplaceholder.typicode.com/posts/5",
"https://jsonplaceholder.typicode.com/posts/6",
"https://jsonplaceholder.typicode.com/posts/7",
"https://jsonplaceholder.typicode.com/posts/8"
];
const allRequests = urls.map(url =>
fetch(url).then( response => response.json() ).then( json => ( {url, json } ) )
);
return Promise.all(allRequests);
};
fetchData().then(r => console.log(r));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question