1

So this next function should search in a pre-stored array, inside chrome storage, a certain index equals to the current date:

function comparingDates(fn) {
var currentDate = new Date();
var j;
chrome.storage.sync.get("storeDates", function comparing(items) {
        if (!chrome.runtime.error) {
            var arrayDates = items.storeDates;
            j = arrayDates.findIndex(x => x.date == currentDate.getDate());
            console.log(j);
           fn(j);
        };
   });
};

With fn being the function that will send the corresponding index found to this callback:

comparingDates(function (l1) {
        if (!l1) {
            console.log("error");
        } else {
           currentDay = l1;
            console.log("succes");
        };

the variable currentDay is declared before.

After the function is run and the value is sent, takes the condition when l1 is undefined.

Not knowing full well how to handle async functions is difficult to get a good grasp were the error occurs. And doing some search about it, doesn't provide with good examples for pratical references.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • Well you are missing a closing ) and findIndex returns -1 for not found so if item is in first index it would be an "error" and not found is valid?? – epascarello Jul 21 '17 at 13:31
  • @epascarello when running the code, the console.log(j) pass the correct index. So that part is not a problem i think. anyhow can you show me how you would solved the problem. – Andres Payema Jul 21 '17 at 13:38
  • I'm not sure what the phrase "takes the condition when `l1` is undefined" means. Do you mean that when you debug it, the `li` argument is `undefined`? – Heretic Monkey Jul 21 '17 at 13:38
  • @MikeMcCaughan yeah, exactly that. Sorry for not knowing how to express myself. – Andres Payema Jul 21 '17 at 13:42
  • So your problem is not what you have above, it is code that is not shown.... – epascarello Jul 21 '17 at 13:44
  • No problem. How are you identifying `l1` is `undefined`? Do you have a `console.log(l1)` in there somewhere? It's hard for us, of course, since we don't have your data. – Heretic Monkey Jul 21 '17 at 13:48
  • @MikeMcCaughan when passing 'l1' from comparingDates, i do a check of the variable with '!l1'. if it returns truth i get a 'console.log("error")'. if not truth it assign l1 to currentDay. – Andres Payema Jul 21 '17 at 14:14
  • Yeah, I can read your code. But "truth" is in the eye of the beholder. In the case of JavaScript, `null`, `undefined`, `0`, and a number of other things evaluate to false when used in a condition. It is important for us to know the exact value of the `l1` variable. Add a `console.log(l1)` at the top of the function you pass to `comparingDates` (in your second code block). – Heretic Monkey Jul 21 '17 at 14:17
  • @MikeMcCaughan i see the problem them, the index value pass is 0 this time – Andres Payema Jul 21 '17 at 14:20

1 Answers1

1

findIndex returns -1 for not found so if you were to do

if (-1) console.log("Is Truthy")

You would get the console line. And if the item is in the first index (aka 0) than the check would also fail.

You should be checking for -1, not truthy.

  comparingDates(function (l1) {
    if (l1===-1) {
      console.log("error");
    } else {
      currentDay = l1;
      console.log("success");
    }
  });

And if you are saying this

  var l1;
  comparingDates(function (l1) {
    if (l1===-1) {
      console.log("error");
    } else {
      currentDay = l1;
      console.log("success");
    }
  });
  console.log(l1) <-- is undefined

Than you have the problem where you order a pizza and soon as you hang up the phone, you try to eat the pizza. The code after the function does not sit and wait for the callback to run. That is why we have the callback to wait for the order to be delivered. The logic needs to be done there.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • LOL @ pizza analogy. It's really good except for the fact that in this case, because you're not waiting asynchronously on an HTTP request or database query or anything like that, it's not quite like ordering pizza because the pizza delivery guy controls when your pizza arrives, but here you control when you call the function to resolve the value, right? It would be more like having your own pizza in the oven and wondering why it's not on the table yet when you forgot to take it out of the oven. – anandsun Jul 21 '17 at 13:49
  • `chrome.storage.sync.get("storeDates"` is the delivery guy in his beat up ford escort with missing exhaust pipes but has nice rims with spinners. – epascarello Jul 21 '17 at 13:53