0

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"))
  );
John Deacon
  • 183
  • 3
  • 13
  • 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. – Pointy Jul 26 '20 at 20:54
  • 1
    Does this answer your question? [How do JavaScript closures work?](https://stackoverflow.com/questions/111102/how-do-javascript-closures-work) – Siguza Jul 26 '20 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. – jarmod Jul 26 '20 at 20:55
  • Thank you all. This is helpful to me. I'm reading the linked question right now – John Deacon Jul 26 '20 at 22:08

1 Answers1

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();
  }
});
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60
  • 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! – John Deacon Jul 27 '20 at 01:38