How to get promises working in an embedding

16 views
Skip to first unread message

Miles

unread,
Dec 2, 2020, 12:01:27 PM12/2/20
to
Hi,
I'm hoping someone can help point me in the right direction here.
We have been updating our embedding to ESR 78 and it's all working nicely so far. One reason for doing this though is to be able to use ES6 features in our embedding and I've started looking at promises.
I've picked up a simple example from the web and tweaked it to work in our embedding (Message is our equivalent of console.log)

const myPromise = new Promise((resolve, reject) => {
if (Math.random() * 100 < 70) {
Message('resolving the promise ...');
resolve('Hello, Promises!');
}
reject(new Error('In 30% of the cases I fail.'));
});

// Two functions
const onResolved = (resolvedValue) => Message(resolvedValue);
const onRejected = (error) => Message(error);

myPromise.then(onResolved, onRejected)

This doesn't work though. The only message I get is the 'resolving the promise ...' one. I never get the resolve or reject message.
I'm assuming that there is something extra I have to do to implement promises in our embedding.
I currently use the debugger API in our embedding so I have a call to
js::UseInternalJobQueues(cx);
which is required to get that to work and I had hoped this would magically mean that promises would work here too.
Clearly there is something that I'm missing though. I'm guessing it means that I need to do something with a job queue/event loop to 'process' the promises but I have no idea what.
Can anybody point me in the right direction?

Many thanks

Miles

Steve Fink

unread,
Dec 2, 2020, 12:44:19 PM12/2/20
to Miles, dev-tech-...@lists.mozilla.org
On 12/2/20 9:01 AM, Miles wrote:
> I'm assuming that there is something extra I have to do to implement promises in our embedding.
> I currently use the debugger API in our embedding so I have a call to
> js::UseInternalJobQueues(cx);
> which is required to get that to work and I had hoped this would magically mean that promises would work here too.
> Clearly there is something that I'm missing though. I'm guessing it means that I need to do something with a job queue/event loop to 'process' the promises but I have no idea what.
> Can anybody point me in the right direction?

First, I don't know.

Second, it looks like from both shell/js.cpp and
https://searchfox.org/mozilla-central/source/js/public/Promise.h#561-565
that you need to be calling js::RunJobs(cx) periodically, probably when
returning from a toplevel JS invocation. So I'm guessing that's the
missing bit?

It may be that for your embedding you'll want to switch over at some
point to using InitDispatchToEventLoop to better integrate with your
event loop, and improve the semantics -- as in, have tasks running at
the right time, better matching HTML semantics. (I think it's HTML, not
JS?) But for now, I think you're probably right that using the internal
job queue is easier to start with.


Reply all
Reply to author
Forward
0 new messages