Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making multiple fetch api calls how to check if all calls have finished? [duplicate]

I am grabbing content from multiple urls. Fetch api uses promises all over.

So my code for one requests looks like this

fetch(url).then((response)=>response.text()).then(function(html) { //stuff });

now i have array of urls and multiple calls will be made how do i know if all calls have finished.

i tried using Promise.all but if you see there are two promises for every request. Is there a better way, also Promise.all support is not that good either.

like image 790
Muhammad Umer Avatar asked Jul 01 '16 17:07

Muhammad Umer


1 Answers

Assuming you have an array of urls named urls

// separate function to make code more clear
const grabContent = url => fetch(url)
     .then(res => res.text())
     .then(html => (/* process html here */))

Promise
    .all(urls.map(grabContent))
    .then(() => console.log(`Urls ${urls} were grabbed`))
like image 197
Yury Tarabanko Avatar answered Oct 05 '22 22:10

Yury Tarabanko