-1

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"))
  );
4
  • 2
    A return statement 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 the if test. Commented Jul 26, 2020 at 20:54
  • 1
    Does this answer your question? How do JavaScript closures work? Commented Jul 26, 2020 at 20:55
  • 4
    The return statement doesn't return from the outer getRecommendations function. It returns from the inner, anonymous function caused by your then statement. Commented Jul 26, 2020 at 20:55
  • Thank you all. This is helpful to me. I'm reading the linked question right now Commented Jul 26, 2020 at 22:08

1 Answer 1

1

browser.storage.local.get returns a promise that is executed asynchronously. The function that contains your return statement is executed after the rest o the block of code and can not have any influence of the execution of the rest of this block. You will need to place the rest of the block in a function and change the code to

browser.storage.local.get("domains").then(({ domains }) => {
  const domain = parseDomain(url);
  if (domains.includes(domain)) { 
    callUrlFunction();
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, Adrian, this makes a lot of sense and I hadn't caught that asynchronous execution, so you've done a huge favor to me in pointing out this solution!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.