Callback works, but refactoring webRequest listener to promise throws exceptions

(Code is linked below)

Background

For one of my add-ons I’ve made a small wrapper around the browser.webRequest.* listener…

It e.g. provides some features such as a timeout and is intended for “attach after starting request to control whether it succeeded”, i.e. detach afterwards, so the “livespan”" is quite short. :smiley:
It is also only intended to capture one (and only one) request. And the add-on itself triggers/starts the request, so effectively it is just a control system to catch potential errors.

Now currently it used a callback for actually signalling a success after catching that one request and passing data back. However, it was already written in a Promise (maybe I’ve already refactored it?) and so it was also wrapped properly in new fresh ES6 code with a promise.
That promise is e.g. rejected when the timeout occurs.

So the rejection works without problems in the timeout case. And generally, also the success works.
But when I’ve now wanted to change it completely and get rid of the old callback to just replace it with the .then handler, which is actually quite simple as I only need to remove the callback and adjust the usages of the function, because the .then/Promise handling was already there, as said, I thought it would work without problems.

But it turned out Firefox claims now that the Promise functions (i.e. resolve and reject) would be undefined. Which is clearly wrong, as I can even see that they are defined in the debugger there… :thinking:
I also checked this only triggers once (actually the listener is quickly removed after a success) and I see no scope problems here.

So this is really a riddle for me. It would be great, if you could help me.


But here now the quick and formal bug report, that I’ve also tracked in my repo for now:

Problem

Bug description

When I try to use 100% promises for the NetworkHelper.waitForWebRequest, by refactoring the callback into a .then promise block, Firefox always throws an error:

(Thus I had to revert this later..)

Steps to reproduce

Apply this diff:

Actual behavior

Work as before.

Expected behavior

I get an exception that claims the resolve function of the Promise is not defined, it’s undefined. Which cannot be true, because it is certainly in the scope and I can even see it in the Firefox debugger. :smiley:

Isn’t the error that you can’t call catch on undefined, which is what resolve returns?

1 Like

Ahhhh, thanks. Of course… :man_facepalming: