Problem with return

1

I'm having problem with return since node is all asynchronous.

var myFUNC = function(A, B) {
    A.every(function(AA) {        
        return (AA === B);
    });

    return true;
};

if(!myFUNC(...)) {
    ....
}

Of course the function always returns true but it should not do this, as I'm starting with node , I do not know how to solve this impasse.

It should follow the following logic:

  • scans array A
  • If AA === B is false it stops by array and returns false
  • If no item of array returns false then returns true
asked by anonymous 02.11.2015 / 17:24

1 answer

7

The problem with your code is not that it is running asynchronously. The problem is that you're ignoring the result of calling A.every(...) (which runs synchronously) - if all return (AA === B) are true, then every returns true (or false if any of them are not true).

You can rewrite your function as follows:

var myFUNC = function(A, B) {
    var todosIguais = A.every(function(AA) {        
        return (AA === B);
    });

    return todosIguais;
};

if(!myFUNC(...)) {
    ....
}
    
02.11.2015 / 17:59