I know this question is not the first asked here but I tried to work with other answers but my code is not working as expected. I have a Promise.all where I have multiple fetch. The result of the fetch I turn into a json and then goon. When everything is done it should print "done" and of course more code to come. However, what happens is, that "done" is printed right away and then one by one the result of the fetches come in.
Here is my code:
Promise.all(selectedTypes.map((type) => {
let body = {
selectedScreenshot: type,
dataUrl: dataUrl
};
fetch(URL, {
method: 'POST',
body: JSON.stringify(body),
credentials: 'same-origin',
})
.then(resp => {
console.log(resp.json()); // this is done one by one as the results come in
console.log('next');
})
}
)).then(text => {
console.log('done'); // this should be printed last, is printed first
})
What am I doing wrong?