This works as long as the returned promise is not destroyed before it resolves (aka canceled). You would want to document the function with something like "it is not safe to cancel the returned promise -- you must wait for it to complete". This is arguably ugly since the default assumption is that all promises are safe to cancel.
Another problem is that if the fulfiller is never called, the promise deadlocks. Normally, if a fulfiller is destroyed without fulfill() or reject() having been called, then the system automatically calls reject() to prevent this. But if the fulfiller is attached to its own result promise then of course this won't happen. But yes, aside from these issues, it should work. -Kenton On Mon, Jun 13, 2016 at 9:30 AM, Nathan Hourt <[email protected]> wrote: > Is it safe to do something like this: > > auto paf = kj::newPromiseAndFulfiller<T>(); > beginAsyncTask(paf.fulfiller.get()); > return paf.promise.attach(kj::mv(paf.fulfiller)); > > I think that should be fine, as long as beginAsyncTask eventually resolves > or rejects the supplied fulfiller, right? The context here is that I'm > resolving the fulfiller within a lambda that gets stored in a > std::function, and if I capture the Own<PromiseFulfiller> in a lambda, > std::function won't work. I welcome better ways to address this issue also. > -- > Nathan Hourt > > *The Truth will set you free* > > -- > 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.
