Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #34394 > unrolled thread
| Started by | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| First post | 2017-11-28 11:35 +1100 |
| Last post | 2017-12-02 04:04 -0800 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.javascript
Running promises in order Andrew Poulos <ap_prog@hotmail.com> - 2017-11-28 11:35 +1100
Re: Running promises in order "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-11-28 13:02 +0100
Re: Running promises in order "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-11-28 13:31 -0800
Re: Running promises in order Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-12-01 02:37 +0100
Re: Running promises in order "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-12-01 06:53 -0800
Re: Running promises in order ali.abdulhannan@gmail.com - 2017-12-02 04:04 -0800
| From | Andrew Poulos <ap_prog@hotmail.com> |
|---|---|
| Date | 2017-11-28 11:35 +1100 |
| Subject | Running promises in order |
| Message-ID | <QKqdnRBapfSmMoHHnZ2dnUU7-KHNnZ2d@westnet.com.au> |
How do I run an arbitrary number of promises where the subsequent promise is not to run before the previous promise is fulfilled? The values a previous promise generates is used by subsequent promises. The various examples I've googled have the user triggering the next promise eg. by clicking a button. Andrew Poulos
[toc] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2017-11-28 13:02 +0100 |
| Message-ID | <XnsA83B84B063686eejj99@194.109.6.166> |
| In reply to | #34394 |
Andrew Poulos <ap_prog@hotmail.com> wrote on 28 Nov 2017 in comp.lang.javascript: > How do I run an arbitrary number of promises where the subsequent > promise is not to run before the previous promise is fulfilled? > > The values a previous promise generates is used by subsequent promises. > > The various examples I've googled have the user triggering the next > promise eg. by clicking a button. There must be countless psychological publications about the incertaincy of the fullfillment of promises and their probable timespan. Couldn't you start the execution with a test of a global variable on the fullfullment of the 'former' promise, and the restacking of the latter one on failing of the former? -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | "Michael Haufe (TNO)" <tno@thenewobjective.com> |
|---|---|
| Date | 2017-11-28 13:31 -0800 |
| Message-ID | <984bc0e7-1a08-444d-baac-caa0b7283380@googlegroups.com> |
| In reply to | #34394 |
On Monday, November 27, 2017 at 6:35:22 PM UTC-6, Andrew Poulos wrote:
> How do I run an arbitrary number of promises where the subsequent
> promise is not to run before the previous promise is fulfilled?
>
> The values a previous promise generates is used by subsequent promises.
>
> The various examples I've googled have the user triggering the next
> promise eg. by clicking a button.
At first blush, something like the following:
promiseArray.reduce((prev, curr) => (
prev.then(resultList => (
asyncFunc(curr).then(result => resultList.concat(result))
))
), Promise.resolve([]));
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2017-12-01 02:37 +0100 |
| Message-ID | <4793140.hK7J4vrPBd@PointedEars.de> |
| In reply to | #34396 |
Michael Haufe (TNO) wrote: > On Monday, November 27, 2017 at 6:35:22 PM UTC-6, Andrew Poulos wrote: >> How do I run an arbitrary number of promises where the subsequent >> promise is not to run before the previous promise is fulfilled? >> >> The values a previous promise generates is used by subsequent promises. >> >> The various examples I've googled have the user triggering the next >> promise eg. by clicking a button. > > At first blush, something like the following: > > promiseArray.reduce((prev, curr) => ( > prev.then(resultList => ( > asyncFunc(curr).then(result => resultList.concat(result)) > )) > ), Promise.resolve([])); That is a very clever (mis)use of Array.prototype.reduce() :) -- PointedEars FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix> <https://github.com/PointedEars> | <http://PointedEars.de/wsvn/> Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
[toc] | [prev] | [next] | [standalone]
| From | "Michael Haufe (TNO)" <tno@thenewobjective.com> |
|---|---|
| Date | 2017-12-01 06:53 -0800 |
| Message-ID | <5d3dfded-4479-4857-b037-847a3529252c@googlegroups.com> |
| In reply to | #34397 |
Thomas 'PointedEars' Lahn wrote:
> Michael Haufe (TNO) wrote:
>
> > On Monday, November 27, 2017 at 6:35:22 PM UTC-6, Andrew Poulos wrote:
> >> How do I run an arbitrary number of promises where the subsequent
> >> promise is not to run before the previous promise is fulfilled?
> >>
> >> The values a previous promise generates is used by subsequent promises.
> >>
> >> The various examples I've googled have the user triggering the next
> >> promise eg. by clicking a button.
> >
> > At first blush, something like the following:
> >
> > promiseArray.reduce((prev, curr) => (
> > prev.then(resultList => (
> > asyncFunc(curr).then(result => resultList.concat(result))
> > ))
> > ), Promise.resolve([]));
>
> That is a very clever (mis)use of Array.prototype.reduce() :)
I haven't tried it, but I think using the new "async"/"await" keywords with a for...of loop would be clearer for the more traditionally minded, but I try to avoid procedural constructs:
async function foo(promiseList) {
var resultList = []
for(const p of promiseList) {
resultList.push(await doPromise(p));
}
return resultList
};
Looks like there are a handful of alternatives approaches out there as well:
<https://stackoverflow.com/a/24586168/153209>
[toc] | [prev] | [next] | [standalone]
| From | ali.abdulhannan@gmail.com |
|---|---|
| Date | 2017-12-02 04:04 -0800 |
| Message-ID | <e7b971cd-da81-4da2-8cd1-539ae51aee07@googlegroups.com> |
| In reply to | #34394 |
On Monday, November 27, 2017 at 5:35:22 PM UTC-7, Andrew Poulos wrote:
> How do I run an arbitrary number of promises where the subsequent
> promise is not to run before the previous promise is fulfilled?
>
> The values a previous promise generates is used by subsequent promises.
>
> The various examples I've googled have the user triggering the next
> promise eg. by clicking a button.
>
> Andrew Poulos
Happened to check this usenet group, so would like to weigh in. The reduce function is something I would use
<code lang="js">
const pArray = [ p1, p2, p3 ];
const fulfilledResults = [];
pArray.reduce((prev, current) => prev.then((...args) = {
fulfilledResults.push(args);
})));
</code>
However if you have many of those usecases I would suggest you to check out Bluebird library which handles scenarios such as these already. Specifically check the <a href="http://bluebirdjs.com/docs/api/promise.mapseries.html">Promise.mapSeries</a> method
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web