Ok, so your current code is basically this:
function resolved(record) {
web_site_ip = record.addresses[0];
}
function resolved_trr(record_trr) {
web_site_ip_trr = record_trr.addresses[0];
if (web_site_ip == web_site_ip_trr) { /* OK */ } else { /* Oh no!*/ }
}
resolving = browser.dns.resolve(message.domain_name, ["disable_trr", "bypass_cache", "disable_ipv6", "canonical_name"]);
resolving.then(resolved, err);
resolving_trr = browser.dns.resolve(message.domain_name, ["bypass_cache", "disable_ipv6", "canonical_name"]);
resolving_trr.then(resolved_trr, err);
The line with the if condition assumes that the resolved callback was called before the resolved_trr callback, otherwise you are comparing against an empty web_site_ip value (or that of the previous lookup, because you didn’t declare web_site_ip to be a local variable).
Here is my suggestion how to fix that:
First off, the
doing = browser.something.action();
doing.then(onDone, onError);
is a total anti-pattern. It entirely defeats the point of using promises in the first place.
I do not know why the hell all examples on MDN use that pattern.
Instead, you should use await for the value, and catch for errors, where needed.
The following is easyer to understaind and runs everythin in sequence, thus avoiding race conditions:
record = await browser.dns.resolve(message.domain_name, ["disable_trr", "bypass_cache", "disable_ipv6", "canonical_name"]);
record_trr = await browser.dns.resolve(message.domain_name, ["bypass_cache", "disable_ipv6", "canonical_name"]);
if (web_site_ip == web_site_ip_trr) { /* OK */ } else { /* Oh no!*/ }
Bus, as I said, this runs everything in sequence. It first completes one lookup, then starts the other. To start both at once and then wait for booth to resolve, do this:
[ record, record_trr, ] = await Promise.all([
browser.dns.resolve(message.domain_name, ["disable_trr", "bypass_cache", "disable_ipv6", "canonical_name"]),
browser.dns.resolve(message.domain_name, ["bypass_cache", "disable_ipv6", "canonical_name"]),
]);
if (web_site_ip == web_site_ip_trr) { /* OK */ } else { /* Oh no!*/ }
Promise.all takes a list of parallel operations and combines them to one operation. await gets the results, which we directly split up again.
But there is one more problem here: you are not declaring any variables, which means that all the variables will implicitly be global variables. That is pretty messy and can quickly lead to problems if you do multiple things at one (which sooner or later will happen).
Since (during a single lookup) you don’t need to change the values in these variables, yu should declare them as const:
const [ record, record_trr, ] = await Promise.all([
browser.dns.resolve(message.domain_name, ["disable_trr", "bypass_cache", "disable_ipv6", "canonical_name"]),
browser.dns.resolve(message.domain_name, ["bypass_cache", "disable_ipv6", "canonical_name"]),
]);
if (web_site_ip == web_site_ip_trr) { /* OK */ } else { /* Oh no!*/ }
In order to be actually allowed to use the await keyword, this code has to be placed in a async function:
async function handleMessage(message) {
const [ record, record_trr, ] = await Promise.all([
browser.dns.resolve(message.domain_name, ["disable_trr", "bypass_cache", "disable_ipv6", "canonical_name"]),
browser.dns.resolve(message.domain_name, ["bypass_cache", "disable_ipv6", "canonical_name"]),
]);
if (web_site_ip == web_site_ip_trr) { /* OK */ } else { /* Oh no!*/ }
}
It is furthermore a (very) good practice to wrap the code of every file in a self-invoking function (to avoid variable naming conflicts between code in different files), to use strict mode and to catch errors:
(function(global) { 'use strict';
async function handleMessage(message) { try {
const [ record, record_trr, ] = await Promise.all([
browser.dns.resolve(message.domain_name, ["disable_trr", "bypass_cache", "disable_ipv6", "canonical_name"]),
browser.dns.resolve(message.domain_name, ["bypass_cache", "disable_ipv6", "canonical_name"]),
]);
if (web_site_ip == web_site_ip_trr) { /* OK */ } else { /* Oh no!*/ }
} catch (error) {
console.error('dammit', error);
} }
browser.runtime.onMessage.addListener(handleMessage);
})(this);