You can simply use document.body.dataset.theme to detect the current Azure DevOps theme (e.g., "light", "dark", "high-contrast") without making any API or network calls.
This value is injected directly into the DOM by Azure DevOps and stays in sync with the user's theme preferences.