I am using the getDynamicRules method for my chrome extension to see what rules are currently applied, I can get the rules to show in the method like below:
chrome.declarativeNetRequest.getDynamicRules(rules => {
console.log(rules);
})
but I can't assign the rules to an external variable. I'd like to do something like this but it doesn't work:
let rulesArray;
chrome.declarativeNetRequest.getDynamicRules(rules => {
rulesArray = rules
})
It just doesn't seem to assign it, it says the 'rulesArray' is undefined if I try console log it after the method; but if I console log the 'rulesArray' variable in the method after I assign the rules to it then it shows the rules.
I know that the chrome.declarativeNetRequest.getDynamicRules is a promise, I don't really have much experience with JavaScript promises. I've tried using .then in multiple different ways to try and export the rules but nothing has worked. Any help would be much appreciated.
Edit: So this is how I got it to work,in case anyone else is struggling with it: I created a async function for it:
async function getRules() {
try {
return await chrome.declarativeNetRequest.getDynamicRules();
} catch(error) {
console.log(error);
}
}
then when I wanted to call it in another function, I had to make that function an async function and then I called it with an await:
async function exampleFunction() {
const rules = await getRules();
}