On Friday, December 6, 2019 at 9:20:21 AM UTC+11, Geoff Lankow wrote:
> Hi all
> 
> I'm redesigning a bunch of Thunderbird things to be asynchronous. I'd 
> like to use Promises but a lot of the time I'll be far from a JS context 
> so that doesn't really seem like an option. The best alternative I've 
> come up with is to create some sort of listener object and pass it to 
> the async function:
> 
> interface nsIFooOperationListener : nsISupports {
>    void onOperationComplete(
>      in nsresult status,
>      [optional] in string errorMessage
>    );
> };
> 
> ...
> 
> void fooFunction(..., in nsIFooOperationListener listener);
> 
> This works fine but I wonder if there's a better way, or if there's some 
> established prior art I can use/borrow rather than find out the pitfalls 
> myself.
> 
> TIA,
> GL

We have mozilla::MozPromise [0], similar to mozilla::dom::Promise but it 
doesn't rely on JS at all.

It can be a bit tricky to use, the simplest way (to start) is probably to do 
something like InvokeAsync(work thread, code to run that resolves or rejects 
the promise)->Then(target thread, on-success follow-up, on-failure follow-up) 
(e.g., [1]).

Good luck!
g.

[0] 
https://searchfox.org/mozilla-central/rev/ea63a0888d406fae720cf24f4727d87569a8cab5/xpcom/threads/MozPromise.h#98
[1] 
https://searchfox.org/mozilla-central/rev/ea63a0888d406fae720cf24f4727d87569a8cab5/dom/media/ChannelMediaDecoder.cpp#392
_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to