-1

I am trying to learning to asynchronous logic and i have big problems in my head.

I know that almost every function in Node.js is asynchronous. I think our node functions are written this way, so the pointer assignment happens in the event loop. (Node.js up to Posix single-threaded). So event-loop (with help of written asynchronous functions) allows node js to run non-block.

As far as I know, we have to write functions asynchronously.(at least this will be useful).

Question 1: Should we write asynchronously the functions we write in Node.js? Would it be useful if I wrote it?

If so, does it help the event loop? What if we do not write asynchronously? (One function per second or one function per minute)

I learned that a callback should be used. Then I learned just asynchronization can not be achieved using callback (s:code1). Then I learned that nextTick or should use a function like setTimeout. Should we use nexttick yada settimeout in nodejs?

So: code1: it's synchronous

function sum(x, y, cb) {
  for (let index = 0; index < 100000000; index++) {}
  cb(x + y);
}

console.log(0);

sum(1, 2, cb => {
  console.log(cb);
});

console.log(10);

// 0, 3, 10

code2: it's async using (nextTick or setTimeout doesnt matter).

function sum(x, y, cb) {
  process.nextTick(() => {
    for (let index = 0; index < 100000000; index++) {}
    cb(x + y);
  });
}

console.log(0);

sum(1, 2, cb => {
  console.log(cb);
});

console.log(10);
// 0, 10, 3

Question 2: Different process.nextTick - setTimeOut

exam 1: this is sync

function sum(x, y, callback) {
  process.nextTick(() => {
    for (let index = 0; index < 10000000000; index++);
    callback(x + y);
  });
}

var mul = (x, y) =>
  new Promise((resolve, reject) => {
    resolve(x * y);
  });

sum(2, 3, cb => {
  console.log(cb);
});

mul(5, 5).then(cb => {
  console.log(cb);
});

exam2: it's async

function sum(x, y, callback) {
  setTimeout(() => {
    for (let index = 0; index < 10000000000; index++);
    callback(x + y);
  }, 0);
}

var mul = (x, y) =>
  new Promise((resolve, reject) => {
    resolve(x * y);
  });

sum(2, 3, cb => {
  console.log(cb);
});

mul(5, 5).then(cb => {
  console.log(cb);
});

Why ?

Thank you for the enlightening answers. Can you suggest me any doc or education about this topic.

Thanks.

1 Answers1

3

Functions like process.nextTick() and setTimeout() don't actually make code run asynchronously. Instead, they change the timing of when the code runs, but when it runs its still synchronous. So, you can use those functions to postpone when something runs to some time later. For example, in either of your sum() functions, if that giant for loop takes 5 seconds to run, it's still going to block the event loop for 5 seconds whether you run it immediately or whether you postpone when it runs with process.nextTick() or with setTimeout(). All those functions do is change when it runs. Sometimes changing the timing of when it runs is desirable, but it doesn't make something non-blocking.

You can't actually write a from-scratch asynchronous function in pure Javascript. Javascript in node.js is single threaded so any Javascript you write will, by definition, not run in the background. To be truly asynchronous, an asynchronous function has to actually leverage native code where it can use threads or non-blocking OS functions.

So, to actually run that for loop asynchronously (where your other Javascript could run at the same time that for loop was running), you'd have to either use native code to run it with native code in a thread or fire up a child node.js process to run it in another node.js process and let the OS manage the concurrency.

You can, of course, write Javascript that calls asynchronous functions that have already been written in native code (such functions in the fs or http modules), but even then if you do anything time consuming in your Javascript (like to process a large result), that will still take time and other Javascript can't run while you're running the Javascript processing that large result.


Asynchronous code is pretty much always more complicated to write, test and debug than synchronous code. So, you rarely want to take something that is actually blocking and synchronous and put it behind an asynchronous API as that just makes thing more complicated than need be.

If you need to use process.nextTick() or setTimeout() to schedule something to run some time in the future, that's perfectly fine, then it would make sense to put it behind a promise interface.

But, if there's no obvious benefit to your code structure by postponing when you run it, then putting it behind a promise interface just makes it more complicated to use. Synchronous code is just simpler. One of my top 5 rules of programming is to not make anything more complicated than it needs to be (often referred to as KISS - keep it simple stupid). That applies here.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I realized that I confused parallelism with asynchronism. Thanks to your response, I am very close to understanding :) thx again. –  Apr 05 '18 at 11:34