I didn't write this originally, so I apologize for gaps in my knowledge.
I've added the following block of code to determine if a user has navigated to one of the domains contained in an array. This array is defined as 'domains.' Only if the user's current domain, parsed from Url, is a match, should the rest of the function be executed. (my addition starred).
The problem is that the function is still executing regardless of what Url the user lands on, even with this 'if' condition in place. I know this because the recommendations api fires regardless of what site I navigate to. I'm not sure it's a placement issue or if my syntax is incorrect (or both); but I'd greatly appreciate any insight on this!
export const getRecommendations = url =>
**browser.storage.local.get("domains").then(({ domains }) => {
const domain = parseDomain(url);
if (!domains.includes(domain)) {
return
}
});**
browser.storage.local.get("user").then(({ user }) =>
fetch(trestle.api.recommendations, {
method: "POST",
body: JSON.stringify({ currentUrl: url, emailAddress: user.username }),
headers: {
Authorization: `Bearer ${user.token}`,
Accept: "application/json",
"Content-Type": "application/json"
}
})
.then(response => response.json())
.then(recommendation => recommendation)
.catch(e => new Error("Unable to return recommendations for company"))
);
returnstatement exits the function in which it exists, not all containing functions. You've got a.then()clause with a function, so that's the function that exits. Basically the code as written does not do anything useful, as the function will exit one way or the other regardless of theiftest.returnstatement doesn't return from the outergetRecommendationsfunction. It returns from the inner, anonymous function caused by yourthenstatement.