Hi Amit,

Yeah, I'll avoid changing the behavior of joinPromises() -- if I implement
the new behavior I'll make it a new function joinPromisesFailfast().

That said, for the use case you describe, I'd suggest an RAII-style design.
That is, cleanup/teardown should always happen in destructors, not in
exception handlers. The biggest reason for this is that a catch handler
won't ever run if the top-level promise is canceled (dropped/destroyed),
whereas destructors will run. But also, you could probably come up with a
design where the correct cleanup would happen whether or not joinPromises()
failed fast or waited for all promises to finish.

-Kenton

On Wed, Jun 21, 2017 at 11:38 PM, <[email protected]> wrote:

> Hi Kenton,
>
> Sorry to bring this up from a long time ago, but if it's possible I think
> the behavior of joinPromises should be well-defined. For my use case, which
> I share below, it's preferable for all promises to complete before an
> exception is propagated, but I understand the reasons to go the other way.
>
> Here's my use case:
> For each successful promise in the array I would like to call an "undo"
> promise in case one of the others fail. So I can write code similar to this:
> kj::Vector<kj::Promise<void>> vec;
> std::shared_ptr<Cleanup> cleanup; // This is like an "async guard" to
> undo successful promises in case one fails
> for (p in promises) {
>   vec.add(p.then()[cleanup] { cleanup->add(undo(p)); });
> }
> kj::joinPromises(vec.releaseAsArray()).catch_([cleanup](){
>     cleanup->Go(); // Calls all added undo promises
>  });
>
>
> If an exception is called after all promises complete (successfully or
> unsuccessfully) - I believe this code is correct.
> However, if a single failure propagates immediately - this code is
> incorrect, as one promise can be halfway to successful completion and the
> cleanup won't be called for it when it completes.
>
> Thanks,
> Amit
>
> On Saturday, November 14, 2015 at 1:00:08 AM UTC+2, Kenton Varda wrote:
>>
>> Yes, an exception from any one promise becomes an exception from the
>> combined promise.
>>
>> I think joinPromises() still waits for all to complete before propagating
>> the exception. Arguably it should cancel all the other promises as soon as
>> one resolves to an exception.
>>
>> -Kenton
>>
>> On Fri, Nov 13, 2015 at 2:26 PM, Nathan Hourt <[email protected]> wrote:
>>
>>> If I use kj::joinPromises to convert my Array<Promise<T>> to
>>> Promise<Array<T>>, and one of the promises breaks, what happens to the
>>> joined promise? Does it break and thereby throw away all of the resolved
>>> promises?
>>>
>>> Thanks!
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Cap'n Proto" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> Visit this group at http://groups.google.com/group/capnproto.
>>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Cap'n Proto" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> Visit this group at https://groups.google.com/group/capnproto.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Cap'n Proto" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
Visit this group at https://groups.google.com/group/capnproto.

Reply via email to