Re: [osgi-dev] Converting CompletableFuture to Promise
Thanks Tim, I had actually put together a version almost similar using whenComplete. I must be starting to get the hang of it ;), but I must still admit that your implementation addresses a few holes in mine. Alain On Mon, Oct 29, 2018 at 5:58 AM Tim Ward wrote: > Hi Alain, > > Unsurprisingly this isn’t very hard to do: > > private PromiseFactory pf; > > public Promise toPromise(CompletionStage cs) { > Deferred deferred = pf.deferred(); > cs.whenComplete((r,e) -> { > if(e == null) { > deferred.resolve(r); > } else { > deferred.fail(e); > } > }); > return deferred.getPromise(); > } > > public Promise toPromise(CompletableFuture cf) { > if(cf.isDone() && !cf.isCompletedExceptionally()) { > return pf.resolved(cf.getNow(null)); > } else { > return toPromise((CompletionStage) cf); > } > } > > Note that the CompletableFuture version is just a way to optimise when the > Completable Future is already successfully resolved (the API for consuming > failures is so bad that it’s not worth trying to optimise the already > failed case). > > Best Regards, > > Tim > > On 28 Oct 2018, at 15:41, Alain Picard via osgi-dev < > osgi-dev@mail.osgi.org> wrote: > > We are now using Promises all over the place, but we are finding ourselves > using a library that uses CompletableFuture and want our service based on > that library to convert those futures into promises. > > Has anyone done this before? While I can surely find a way of doing it, I > would like to get some best practice advice from the experts. > > Cheers, > Alain > > ___ > OSGi Developer Mail List > osgi-dev@mail.osgi.org > https://mail.osgi.org/mailman/listinfo/osgi-dev > > > ___ OSGi Developer Mail List osgi-dev@mail.osgi.org https://mail.osgi.org/mailman/listinfo/osgi-dev
Re: [osgi-dev] Converting CompletableFuture to Promise
Hi Alain, Unsurprisingly this isn’t very hard to do: private PromiseFactory pf; public Promise toPromise(CompletionStage cs) { Deferred deferred = pf.deferred(); cs.whenComplete((r,e) -> { if(e == null) { deferred.resolve(r); } else { deferred.fail(e); } }); return deferred.getPromise(); } public Promise toPromise(CompletableFuture cf) { if(cf.isDone() && !cf.isCompletedExceptionally()) { return pf.resolved(cf.getNow(null)); } else { return toPromise((CompletionStage) cf); } } Note that the CompletableFuture version is just a way to optimise when the Completable Future is already successfully resolved (the API for consuming failures is so bad that it’s not worth trying to optimise the already failed case). Best Regards, Tim > On 28 Oct 2018, at 15:41, Alain Picard via osgi-dev > wrote: > > We are now using Promises all over the place, but we are finding ourselves > using a library that uses CompletableFuture and want our service based on > that library to convert those futures into promises. > > Has anyone done this before? While I can surely find a way of doing it, I > would like to get some best practice advice from the experts. > > Cheers, > Alain > > ___ > OSGi Developer Mail List > osgi-dev@mail.osgi.org > https://mail.osgi.org/mailman/listinfo/osgi-dev ___ OSGi Developer Mail List osgi-dev@mail.osgi.org https://mail.osgi.org/mailman/listinfo/osgi-dev
[osgi-dev] Converting CompletableFuture to Promise
We are now using Promises all over the place, but we are finding ourselves using a library that uses CompletableFuture and want our service based on that library to convert those futures into promises. Has anyone done this before? While I can surely find a way of doing it, I would like to get some best practice advice from the experts. Cheers, Alain ___ OSGi Developer Mail List osgi-dev@mail.osgi.org https://mail.osgi.org/mailman/listinfo/osgi-dev