0

Here is my code

var x = 0
data.results[0].taxonomies.some(function(e){
    if(taxo.indexOf(e.code)!=-1){ //if at least one code from the data base is in my list of codes
        callback(validLic(data.results[0].taxonomies)) //return true after some other validations
        return true
    }else{
        x++
        return false
    }
})
if(x==data.results[0].taxonomies.length){callback(false)}//if the entire array was processed, and I didn't find anything I was looking for, return false

I'd like someone to confirm that due the async nature of node, the last if statement is at some point, bound to fire off before I'm done processing the array.

How can I better manage this situation without the help of some sync or parallel library?

The reason I ask it that way is because I'm under the impression that if I can't write something to be completely async, then I'm not writing it efficiently, right?

EDIT:

Based on Luc Hendirks' logic, I have changed my code to this:

var flag = data.results[0].taxonomies.some(function(e){
   if(taxo.indexOf(e.code)!=-1){ //if at least one code from the data base is in my list of codes
       return true
   }else{
       return false
   }
})
if(flag==true){
    callback(validLic(data.results[0].taxonomies))
}else{
    callback(false)
}

Because this follows the sync traits outlined below, I shouldn't have an issue with flag being undefined before the callback is called now right?

  • 3
    Where did you get that impression from? – Marvin Feb 03 '16 at 22:27
  • 1
    I have an entire game written to be asynchronous, and it's marvelous. – Sterling Archer Feb 03 '16 at 22:32
  • I've seen hundreds of efficient, "syncronous" implementations of Hello World. Here's one: `echo "Hello world"`. Here's an "ansynchronous" version: `echo "Hello World" &` Which is berrer? – Gavriel Feb 03 '16 at 22:32
  • Why don't you want to use a parallel library? `parallel` is an awesome parallel library, and `async` is great for async code (but not really parallel). `async` has many, really useful methods for your case. – Francisco Presencia Feb 03 '16 at 22:33
  • to answer your question, if `callback` has some async methods then yes, the code is wrong. If it's only sync methods (returning a value), then it's right. – Francisco Presencia Feb 03 '16 at 22:36

1 Answers1

4

Javascript (and Node) are single threaded, meaning it has only 1 CPU available. If the functions you call only require CPU time, making it async is useless. If you need to call a function and the CPU has to wait (do nothing), then making it async is very useful. Because while it is waiting until the function is finished it can do something else.

A function that checks if a url is valid with a regular expression can be synchronous, as the CPU needs to do some calculations and you get the result. If the function actually does a GET request and checks the response code, the CPU has to wait until the response is received. In the meantime it could do something else, so this function should be made asynchronous.

The difference of a synchronous and asynchronous function is that a synchronous function returns a value:

function(a) { return a; }

and an asynchronous function returns the result using a callback function (this is an actual function that you put in as a function argument):

function(callback){
    // Do something that takes time but not CPU, like an API call...
    callback('Some result');
}

A synchronous function is called like this:

var a = something();

Asynchronous like this:

something(function(result){
    console.log(result);
});

So to answer your question, if some() is an asynchronous function, then the last if statement can be executed before the some function is finished. Because: the CPU does not want to wait. It can do other stuff while waiting. That's the power of asynchronous programming. t

It is also important to know that "parallel" does not exist in Javascript/Node. There is only 'doing stuff instead of waiting', like executing multiple API calls at the same time. That is not parallel computing as in using multiple threads.

Here is some more info: What is the difference between synchronous and asynchronous programming (in node.js)

Community
  • 1
  • 1
Luc Hendriks
  • 2,473
  • 13
  • 18