Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

/earth: file system full.


devel / comp.lang.javascript / Re: Pause for series of fetch calls

SubjectAuthor
* Pause for series of fetch callsAndrew Poulos
+* Re: Pause for series of fetch callsJulio Di Egidio
|`- Re: Pause for series of fetch callsJulio Di Egidio
`* Re: Pause for series of fetch callsJon Ribbens
 +- Re: Pause for series of fetch callsJulio Di Egidio
 `* Re: Pause for series of fetch callsAndrew Poulos
  `* Re: Pause for series of fetch callsAndrew Poulos
   `- Re: Pause for series of fetch callsJonas Thörnvall

1
Pause for series of fetch calls

<tuSdndPDh_w9uDv9nZ2dnUU7-IvNnZ2d@westnet.com.au>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16934&group=comp.lang.javascript#16934

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!feeder5.feed.usenet.farm!feeder1.feed.usenet.farm!feed.usenet.farm!tr2.eu1.usenetexpress.com!feeder.usenetexpress.com!tr1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!buffer1.nntp.dca1.giganews.com!nntp.westnet.com.au!news.westnet.com.au.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 20 May 2021 04:02:24 -0500
Newsgroups: comp.lang.javascript
X-Mozilla-News-Host: news://news.iinet.net.au:119
From: ap_prog@hotmail.com (Andrew Poulos)
Subject: Pause for series of fetch calls
Date: Thu, 20 May 2021 19:02:22 +1000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.10.1
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-AU
Content-Transfer-Encoding: 7bit
Message-ID: <tuSdndPDh_w9uDv9nZ2dnUU7-IvNnZ2d@westnet.com.au>
Lines: 16
X-Usenet-Provider: http://www.giganews.com
NNTP-Posting-Host: 110.175.163.114
X-Trace: sv3-BTrJQTNdtC52Dn6djDy07xMfCawjri/URR7r6W4/tFVHGGq5RlYAlk3gvzBwLb2HXJVJeWRf8cT0flX!Fv3rqkJwVxIQxTD+MmYKWTlP/aBHnzmswF/jSBJPzCfawK6jtPB2spaiT1XzD01lCkRihTdNQEON!VLaxgtJkkc1jla9PvbirCqE=
X-Complaints-To: abuse@westnet.com.au
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 1658
 by: Andrew Poulos - Thu, 20 May 2021 09:02 UTC

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.

I set up a "complex" for loop but trying to debug it is doing me head in.

What's a decent way to do this?

Andrew Poulos

Re: Pause for series of fetch calls

<75ae4450-2129-46b3-b389-8a84417311c5n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16935&group=comp.lang.javascript#16935

  copy link   Newsgroups: comp.lang.javascript
X-Received: by 2002:a37:6084:: with SMTP id u126mr4054183qkb.294.1621502686761;
Thu, 20 May 2021 02:24:46 -0700 (PDT)
X-Received: by 2002:a05:6830:4a6:: with SMTP id l6mr3140431otd.72.1621502686495;
Thu, 20 May 2021 02:24:46 -0700 (PDT)
Path: i2pn2.org!i2pn.org!aioe.org!feeder1.feed.usenet.farm!feed.usenet.farm!2.eu.feeder.erje.net!feeder.erje.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.javascript
Date: Thu, 20 May 2021 02:24:46 -0700 (PDT)
In-Reply-To: <tuSdndPDh_w9uDv9nZ2dnUU7-IvNnZ2d@westnet.com.au>
Injection-Info: google-groups.googlegroups.com; posting-host=93.41.99.178; posting-account=F3H0JAgAAADcYVukktnHx7hFG5stjWse
NNTP-Posting-Host: 93.41.99.178
References: <tuSdndPDh_w9uDv9nZ2dnUU7-IvNnZ2d@westnet.com.au>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <75ae4450-2129-46b3-b389-8a84417311c5n@googlegroups.com>
Subject: Re: Pause for series of fetch calls
From: julio@diegidio.name (Julio Di Egidio)
Injection-Date: Thu, 20 May 2021 09:24:46 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Julio Di Egidio - Thu, 20 May 2021 09:24 UTC

On Thursday, 20 May 2021 at 11:02:36 UTC+2, Andrew Poulos 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.
>
> I set up a "complex" for loop but trying to debug it is doing me head in.
>
> What's a decent way to do this?

Set a counter to zero, start your calls, whenever a call ends increment the counter, when the counter reaches the max value reset the counter and restart over...

Julio

Re: Pause for series of fetch calls

<dcc2e269-c196-4b73-9314-44749bdea1can@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16936&group=comp.lang.javascript#16936

  copy link   Newsgroups: comp.lang.javascript
X-Received: by 2002:a37:7d06:: with SMTP id y6mr4078519qkc.472.1621503698249;
Thu, 20 May 2021 02:41:38 -0700 (PDT)
X-Received: by 2002:a9d:6d93:: with SMTP id x19mr2979809otp.117.1621503698019;
Thu, 20 May 2021 02:41:38 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.javascript
Date: Thu, 20 May 2021 02:41:37 -0700 (PDT)
In-Reply-To: <75ae4450-2129-46b3-b389-8a84417311c5n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=93.41.99.178; posting-account=F3H0JAgAAADcYVukktnHx7hFG5stjWse
NNTP-Posting-Host: 93.41.99.178
References: <tuSdndPDh_w9uDv9nZ2dnUU7-IvNnZ2d@westnet.com.au> <75ae4450-2129-46b3-b389-8a84417311c5n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <dcc2e269-c196-4b73-9314-44749bdea1can@googlegroups.com>
Subject: Re: Pause for series of fetch calls
From: julio@diegidio.name (Julio Di Egidio)
Injection-Date: Thu, 20 May 2021 09:41:38 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Julio Di Egidio - Thu, 20 May 2021 09:41 UTC

On Thursday, 20 May 2021 at 11:24:51 UTC+2, Julio Di Egidio wrote:
> On Thursday, 20 May 2021 at 11:02:36 UTC+2, Andrew Poulos 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.
> >
> > I set up a "complex" for loop but trying to debug it is doing me head in.
> >
> > What's a decent way to do this?
>
> Set a counter to zero, start your calls, whenever a call ends increment the counter, when the counter reaches the max value reset the counter and restart over...

P.S. I guess (without seeing your code) you should not be using async/await, the await part serialises execution. Just use the raw Promise: in the 'then' part increment the counter, etc.

Julio

Re: Pause for series of fetch calls

<slrnsacka7.1q1s.jon+usenet@raven.unequivocal.eu>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16937&group=comp.lang.javascript#16937

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!aioe.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jon+usenet@unequivocal.eu (Jon Ribbens)
Newsgroups: comp.lang.javascript
Subject: Re: Pause for series of fetch calls
Date: Thu, 20 May 2021 12:08:39 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <slrnsacka7.1q1s.jon+usenet@raven.unequivocal.eu>
References: <tuSdndPDh_w9uDv9nZ2dnUU7-IvNnZ2d@westnet.com.au>
Injection-Date: Thu, 20 May 2021 12:08:39 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="195a0f335dd99b329c77198f415f0815";
logging-data="12352"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19eajHCj1FGWMB800+sjLolDOYNacdBqbM="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:8KDVRnHDTFgfBuQhYyHQEQRV5gE=
 by: Jon Ribbens - Thu, 20 May 2021 12:08 UTC

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.

Re: Pause for series of fetch calls

<56bbaea0-4653-4b55-9f55-fa74053c1469n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16942&group=comp.lang.javascript#16942

  copy link   Newsgroups: comp.lang.javascript
X-Received: by 2002:a05:622a:1045:: with SMTP id f5mr4794217qte.392.1621514066104;
Thu, 20 May 2021 05:34:26 -0700 (PDT)
X-Received: by 2002:a9d:6d93:: with SMTP id x19mr3477780otp.117.1621514065889;
Thu, 20 May 2021 05:34:25 -0700 (PDT)
Path: i2pn2.org!i2pn.org!paganini.bofh.team!usenet.pasdenom.info!usenet-fr.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.javascript
Date: Thu, 20 May 2021 05:34:25 -0700 (PDT)
In-Reply-To: <slrnsacka7.1q1s.jon+usenet@raven.unequivocal.eu>
Injection-Info: google-groups.googlegroups.com; posting-host=93.41.99.178; posting-account=F3H0JAgAAADcYVukktnHx7hFG5stjWse
NNTP-Posting-Host: 93.41.99.178
References: <tuSdndPDh_w9uDv9nZ2dnUU7-IvNnZ2d@westnet.com.au> <slrnsacka7.1q1s.jon+usenet@raven.unequivocal.eu>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <56bbaea0-4653-4b55-9f55-fa74053c1469n@googlegroups.com>
Subject: Re: Pause for series of fetch calls
From: julio@diegidio.name (Julio Di Egidio)
Injection-Date: Thu, 20 May 2021 12:34:26 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Julio Di Egidio - Thu, 20 May 2021 12:34 UTC

On Thursday, 20 May 2021 at 14:08:46 UTC+2, Jon Ribbens wrote:

> If you want to always wait for them all to complete, then you could do:
>
> const results = await Promise.all(

+1. Sorry, I had forgotten about that.

Julio

Re: Pause for series of fetch calls

<986dnSLWp73Ddjv9nZ2dnUU7-IHNnZ2d@westnet.com.au>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16954&group=comp.lang.javascript#16954

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!aioe.org!feeder1.feed.usenet.farm!feed.usenet.farm!tr1.eu1.usenetexpress.com!feeder.usenetexpress.com!tr1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!buffer1.nntp.dca1.giganews.com!nntp.westnet.com.au!news.westnet.com.au.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 20 May 2021 18:06:05 -0500
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>
From: ap_prog@hotmail.com (Andrew Poulos)
Date: Fri, 21 May 2021 09:06:04 +1000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.10.1
MIME-Version: 1.0
In-Reply-To: <slrnsacka7.1q1s.jon+usenet@raven.unequivocal.eu>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-AU
Content-Transfer-Encoding: 7bit
Message-ID: <986dnSLWp73Ddjv9nZ2dnUU7-IHNnZ2d@westnet.com.au>
Lines: 60
X-Usenet-Provider: http://www.giganews.com
NNTP-Posting-Host: 110.175.163.114
X-Trace: sv3-Wi7hj1kp5vbJmwTOTdLYHnATwEmAsnrBB3aXoQem9JFkRBfCZu7SMxrdLuEFYpU+vos6bsBkMgOmixa!5xt/AfoDwnD7sEEzu2+SNTVQSbZnxfW1/DvFCC2zyy5VU7tERduqaRg3VyU1oP8imxv7lE+MJxZf!8hxVZpRYNcOhmTk7kLyMgkU=
X-Complaints-To: abuse@westnet.com.au
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 3208
 by: Andrew Poulos - Thu, 20 May 2021 23:06 UTC

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');

And the console displays 'done' before any of the fetch responses.

Andrew Poulos

Re: Pause for series of fetch calls

<xtCdndvrbr22azv9nZ2dnUU7-TXNnZ2d@westnet.com.au>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16955&group=comp.lang.javascript#16955

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.snarked.org!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!nntp.westnet.com.au!news.westnet.com.au.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 20 May 2021 18:52:11 -0500
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: ap_prog@hotmail.com (Andrew Poulos)
Date: Fri, 21 May 2021 09:52:09 +1000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.10.1
MIME-Version: 1.0
In-Reply-To: <986dnSLWp73Ddjv9nZ2dnUU7-IHNnZ2d@westnet.com.au>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-AU
Content-Transfer-Encoding: 8bit
Message-ID: <xtCdndvrbr22azv9nZ2dnUU7-TXNnZ2d@westnet.com.au>
Lines: 75
X-Usenet-Provider: http://www.giganews.com
NNTP-Posting-Host: 110.175.163.114
X-Trace: sv3-Ry58zpScoVSDtbyoGUZGfQVzCPh0/RxHnZNiqPv37GKyfoz58DsB/vd5gzgSVgSo8G8lvn4kNU5RBhp!E4NBV/P5DHT5seUCAmuHi34V5gNGjQG9LJ8JEDI69jtG1eKLcQlvnzODefxZVYWdEuIF4ulGYo+x!6jISNabNVlMNZpeTpWxIHiU=
X-Complaints-To: abuse@westnet.com.au
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 3813
 by: Andrew Poulos - Thu, 20 May 2021 23:52 UTC

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

Re: Pause for series of fetch calls

<3e7c7123-1e40-400a-bdf0-c9f9c97db9bfn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16957&group=comp.lang.javascript#16957

  copy link   Newsgroups: comp.lang.javascript
X-Received: by 2002:a37:644c:: with SMTP id y73mr9746065qkb.331.1621578856937;
Thu, 20 May 2021 23:34:16 -0700 (PDT)
X-Received: by 2002:a05:6830:4a6:: with SMTP id l6mr7098344otd.72.1621578856653;
Thu, 20 May 2021 23:34:16 -0700 (PDT)
Path: i2pn2.org!i2pn.org!aioe.org!news.mixmin.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.javascript
Date: Thu, 20 May 2021 23:34:16 -0700 (PDT)
In-Reply-To: <xtCdndvrbr22azv9nZ2dnUU7-TXNnZ2d@westnet.com.au>
Injection-Info: google-groups.googlegroups.com; posting-host=213.112.1.82; posting-account=kxPkPAoAAACjJi8w0gL9bnyznPzdw9HW
NNTP-Posting-Host: 213.112.1.82
References: <tuSdndPDh_w9uDv9nZ2dnUU7-IvNnZ2d@westnet.com.au>
<slrnsacka7.1q1s.jon+usenet@raven.unequivocal.eu> <986dnSLWp73Ddjv9nZ2dnUU7-IHNnZ2d@westnet.com.au>
<xtCdndvrbr22azv9nZ2dnUU7-TXNnZ2d@westnet.com.au>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <3e7c7123-1e40-400a-bdf0-c9f9c97db9bfn@googlegroups.com>
Subject: Re: Pause for series of fetch calls
From: jonas.thornvall@gmail.com (Jonas Thörnvall)
Injection-Date: Fri, 21 May 2021 06:34:16 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Jonas Thörnvall - Fri, 21 May 2021 06:34 UTC

fredag 21 maj 2021 kl. 01:52:23 UTC+2 skrev Andrew Poulos:
> 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_...@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
Yeah it may work but the syntax is mental to do two things in serial..... Just saying....


devel / comp.lang.javascript / Re: Pause for series of fetch calls

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor