0

Suppose I have the following code:

Synchronous Version:

    var waitingToGetValue = heavyProcessingFunc (para1, para2);
    response.send(waitingToGetValue);

I tried looking around but all I got were functions without parameter.

Asynchronous Attempt:

    heavyProcessingFunc(para1, para2, (function(waitingToGetValue){
       response.send(waitingToGetValue);
    });

I need to send the two parameters to the function. The function returns a value that I need to use to then send it to the response.

Does it even matter if I put it in a asynchronous format? Can anyone recommend me a video/source that easily explains the difference synchronous vs asynchronous: I read many material for the past few days and it just keep getting more confusing with promises, callbacks, and trying to access the values outside those functions.

  • 2
    Synchronous means everything stops until the response has been received (generally bad because the UI locks up and becomes unresponsive while you wait). Asynchronous means code continues to process while request is being made. This is better because the UI doesn't get blocked, but does introduce the issue of not knowing when the response will come back. This is why we need callbacks. – Scott Marcus Jun 28 '17 at 20:50
  • Making a function accept a callback doesn't make it asynchronous. It depends on what the function does. E.g. `Array#forEach` also takes a callback but is not asynchronous. It's not clear to me what your question really is about. – Felix Kling Jun 28 '17 at 20:51
  • An "asynchronous format" doesn't help anything. You need to make the function actually do something asynchronously, and if it's only a small delay between rounds of heavy processing. – Bergi Jun 28 '17 at 20:54
  • What is your [actual problem](https://meta.stackexchange.com/q/66377)? – Bergi Jun 28 '17 at 20:55
  • @Bergi It is really two problems. One being the formatting the following code above. The second is just something I can read up on that clearly and easily explain evserything about synchronous and asynchronous nature of javascript. Daniel answered the first question. – the_begging_beginner Jun 28 '17 at 21:01
  • What you should understand from the start is the JavaScript runs in a synchronous environment, always. You cannot have two JavaScript statements executing simultaneously. The asynchronous behavior is gained by making Web API calls that are handled asynchronously by the browser / host environment. – Scott Marcus Jun 28 '17 at 21:09
  • @the_begging_beginner As I said, it has nothing to do with formatting. Synchronous functions can take callbacks just as well. See [Are all Node.js callback functions asynchronous?](https://stackoverflow.com/q/21884258/1048572) for more. – Bergi Jun 28 '17 at 21:12
  • @the_begging_beginner about Daniel's answer: Just delaying something doesn't make a heavy task less heavy. Nor does it make it less blocking during its execution. You've just delayed the problem. You should break the task down into smaller chunks that can be handled in some period of time that you're comfortable with *(if your app freezes that long)*. Then do a timeout *(maybe 10ms)* before processing the next chunk so that the browser has time to do some other stuff. Like rendering, or processing mouse- / keyboard- / load-events, etc. – Thomas Jun 28 '17 at 21:52

1 Answers1

0

    function heavyProcessingFunc(para1, para2, callback){
     console.log('We have two params to work with', para1, para2);
     setTimeout(function(){// Simulating a long request
      callback("Now we send the response by calling the callback"); // This is waitingToGetValue
     },3000);
    }
    
    heavyProcessingFunc("para1", "para2", function(waitingToGetValue){
       console.log(waitingToGetValue);
       // response.send(waitingToGetValue);
    });
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
Daniel
  • 579
  • 2
  • 7