Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #40080
| Subject | Re: Pause for series of fetch calls |
|---|---|
| Newsgroups | comp.lang.javascript |
| References | <tuSdndPDh_w9uDv9nZ2dnUU7-IvNnZ2d@westnet.com.au> <slrnsacka7.1q1s.jon+usenet@raven.unequivocal.eu> <986dnSLWp73Ddjv9nZ2dnUU7-IHNnZ2d@westnet.com.au> |
| From | Andrew Poulos <ap_prog@hotmail.com> |
| Date | 2021-05-21 09:52 +1000 |
| Message-ID | <xtCdndvrbr22azv9nZ2dnUU7-TXNnZ2d@westnet.com.au> (permalink) |
On 21/05/2021 9:06 am, Andrew Poulos wrote:
> On 20/05/2021 10:08 pm, Jon Ribbens wrote:
>> On 2021-05-20, Andrew Poulos <ap_prog@hotmail.com> wrote:
>>> I need to:
>>>
>>> - make a series of fetch calls
>>> - when they all have resolved do some other normal (non-fetch) calls
>>> - make a another series of fetch calls
>>> - when they all have resolved continue
>>>
>>> I've only used async/await in a very basic way and I've been unable to
>>> work out how make everything wait for the fetch calls to resolve before
>>> making other calls.
>>
>> Do you really mean a *series* of fetch calls? i.e. that each one can
>> only begin once the previous one has completed? Or do you mean you
>> have a set of fetch calls that you want to run in parallel?
>>
>> If it's the former then:
>>
>> const urls = [ "https://example.com/", "https://example.org/" ... ]
>> const results = []
>> for (const url of urls) results.push(await fetch(url))
>>
>> If it's the latter then it depends on what you want to happen if any
>> of the fetches fail. If you want to give up as soon as any of them
>> fail, do:
>>
>> const results = await Promise.all(urls.map(url => fetch(url)))
>>
>> If you want to always wait for them all to complete, then you could do:
>>
>> const results = await Promise.all(
>> urls.map(url => fetch(url).catch(err => err)))
>>
>> and then results will each be a fetch result or an error.
>
> Sorry but I just don't get it :-(
>
> I tried this
>
> const urls = ['upload/test.php','...];
>
> const results = async () => {
> await Promise.all(
> urls.map(url => fetch(url)
> .then(response => response.json())
> .then(data => {
> console.log('Success: ', data);
> })
> .catch(err => err)));
> };
>
> results();
>
> console.log('done');
Doh, it works if I write it correctly
const results = async () => {
await Promise.all(
urls.map(url => fetch(url)
.then(response => response.json())
.then(data => {
console.log('Success: ', data);
})
.catch(err => err)));
console.log('done');
};
results();
Andrew Poulos
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Pause for series of fetch calls Andrew Poulos <ap_prog@hotmail.com> - 2021-05-20 19:02 +1000
Re: Pause for series of fetch calls Julio Di Egidio <julio@diegidio.name> - 2021-05-20 02:24 -0700
Re: Pause for series of fetch calls Julio Di Egidio <julio@diegidio.name> - 2021-05-20 02:41 -0700
Re: Pause for series of fetch calls Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-05-20 12:08 +0000
Re: Pause for series of fetch calls Julio Di Egidio <julio@diegidio.name> - 2021-05-20 05:34 -0700
Re: Pause for series of fetch calls Andrew Poulos <ap_prog@hotmail.com> - 2021-05-21 09:06 +1000
Re: Pause for series of fetch calls Andrew Poulos <ap_prog@hotmail.com> - 2021-05-21 09:52 +1000
Re: Pause for series of fetch calls Jonas Thörnvall <jonas.thornvall@gmail.com> - 2021-05-20 23:34 -0700
csiph-web