Assuming adequate permissions in the manifest file, why does the following notification code work when stepped through the debugger, but fails when running up to speed? I’m doing something wrong with promises, but I don’t know what.
// S: Promise to show string in notification box
function S(Msg,Title)
{
var p=browser.notifications.create({
“type”: “basic”,
“iconUrl”: browser.extension.getURL(“icons/link-48.png”),
“title”: Title,
“message”: Msg});
return p;
} // S
S(‘bg running 0’,‘Direct from bg’).then(s1).then(s2).catch(err);
function s1(val)
{
S(‘bg 1’,’’);
} // s1
function s2(val)
{
S(‘bg 2’,’’);
} // s2
function err(Msg)
{
console.log(’*** '+Msg);
throw Error;
} // err
// End
I apologize for leaving this out; I thought that it was short enough that people would try it out. I tend to leave things out when I’m anxious and frustrated with programming, which I normally love.
There are three calls here to create notification boxes, numbered 0, 1, and 2. These boxes should appear on the screen of the active tab, one after another. And they do appear if one steps through the code slowly in the debugger. But when run up to speed (by reloading the active tab, either with debugger showing or not), the notification boxes do not appear.
It seems obvious that the reason the notification function returns a promise is that it requires time to display a box because it is operating asynchronously. So, what is wrong with my code that doesn’t give it the time it needs to display?
Thank you for elaborating on what you’re seeing and what you’re expecting. You’re showing notifications with Firefox, so this is unrelated to the active tab. As the documentation for notification.create mentions:
If you call notifications.create() more than once in rapid succession, Firefox may end up not displaying any notification at all.
Which is the case here. Your s1 and s2 functions do not return their promises, which means these notification creations will be even faster than they would be if you waited for the respective promises. But even if you waited for the Promises afaik it’s not guaranteed that all notifications are shown.
This code you’ve posted works for me . I’ve just executed it in the console here.
There is probably some error before that code… check also browser console for errors - browser console is a special console, you can open it with Ctrl + Shift + J.
Also your timeout promise looks a bit complicated, you use a simpler one:
const timeoutPromise = delay => new Promise(resolve => setTimeout(resolve, delay));
There is no other code. But your “simpler” function just seems to omit the bind, and it will not run on all browsers because of the ES6 syntax. I think you are saying
I think the reason that bind is not necessary is that we don’t care what value is in “this”. That is the main reason for using bind, for making sure “this” is correct.