How to get promises working in an embedding
Miles <[email protected]> Wed, 2 Dec 2020 09:01:25 -0800 (PST)
| Newsgroups | gmane.comp.mozilla.devel.jseng |
|---|---|
| Message-ID | <[email protected]> |
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