Say you use webRequest in a manifest v3, policy-installed, enterprise extension. Each request is either canceled, redirected, or allowed, but the decision has to be made based on the result of an API request, which you then cache for similar requests in the future.
chrome.webRequest.onBeforeRequest.addListener(
(details) => mustBeSynchronous(details), { urls: [ '<all_urls>'] }, ["blocking"]
);
mustBeSynchronous(details) : chrome.webRequest.BlockingResponse {
// Must use async fetch, cannot use sync XMLHttpRequest
fetch('https://our.api.endpoint/param?uuid='+ourLookup)
.then(response => response.json())
.then(data => { //This returns a promise anyway, not a chrome.webRequest.BlockingResponse
return makeDecisionsAbout(data);
});
// Returns as soon as fetch is sent, before promise resolves.
// tooLate cannot depend on makeDecisionsAbout()
return tooLate;
}
Without a synchronous request, the page will load before a decision can be made. On subsequent requests a cached result can be used synchronously and locally to make the decision, but the first load of any page will bypass our logic.
I understand that asynchronous requests are better for performance, but in this instance we speciically need a synchronous request for exactly the reason it's shunned; we need to hold things up. I understand discouraging the use of synchronous requests, but banning the function entirely seems a bit Procrustean.