0

So I got this problem for my chrome extension:

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;
        var i = 0;
        do {
            if (arrayDates[i].date == currentDate.getDate()) {
                j = i; // problemas al enviar el j
            };
            i++
        } while (!j);
        fn(j);
        };
    });
};
var currentDay;
 comparingDates(function (l1) {
    if (!l1) {
        console.log("error");
    } else {
        currentDay = l1;
        console.log("succes");
};

storeDates object contains multiple dates, which are compared with the currentDate. When the do/while loops find a match it suppose to assign j a value. But for some reason when the callback is sent the assignment for currentDay is not made.

I try to get a hold of the callback theory and implemented to my code, but mostly all the examples i could find just print the result instead to assign to a certain value. Any suggestions on how to fix this problem

  • You have some syntax errors, fixing them may help solve your issue. – evolutionxbox Jun 26 '17 at 16:17
  • @evolutionxbox can you point out which errors you mean? ty in advanced – Andres Payema Jun 26 '17 at 16:22
  • You have mismatched brackets and you don't close `comparingDates` – evolutionxbox Jun 26 '17 at 16:27
  • arrayDates won't be available when you call "fn" with the array index, maybe that doesn't matter. If currentDate doesn't match anything in arrayDates it will produce an error when trying to read the date property of `arrayDates[i]` with i == arrayDates.length. I suggest you use either array.find or array.findIndex instead of your i, j loop structure. – James Jun 26 '17 at 16:28
  • 3
    Since chrome.* API callbacks are asynchronous, the `currentDay` variable will be only defined inside the callback. Or in the code that runs in subsequent JS engine loop events and resides in the same closure as the variable. See [How do I return the response from an asynchronous call?](//stackoverflow.com/q/14220321) – wOxxOm Jun 26 '17 at 19:03
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Daniel Herr Jun 26 '17 at 19:19
  • I'm glad you found a solution to your problem. However, an actual answer/solution should **not** be edited into your Question. In general, you should [edit] the Question to *clarify the Question*, but not to include an Answer within the Question. You should create your own Answer with the code you used to solve your problem, then accept it (the system may require a 48 hour delay prior to accepting your own answer). When you have solved the problem yourself, [answering your own question is encouraged](http://stackoverflow.com/help/self-answer). – Makyen Jun 27 '17 at 00:21

1 Answers1

0

After looking at the comments i find the solution to the problem:

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;
    var j = arrayDates.findIndex(x => x.date == currentDate.getDate());
    console.log(j);
    fn(j);
        };
    });
};

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