Monday 21 May 2018

Is a self-created function an asynchronous code in Node?

It is known that Node provides some API that run asynchronously for I/O or CPU-intensive tasks, such as fs.readFile function. However, if I self-create a function and then run it with a callback pattern, is it asynchronous (non-blocking)?Consider this function:function addNumber(number, fn) { //Add 1 to 'number' ten times then return the number for (let i = 0; i < 10; i++) { number += 1; } fn(number); } If I then call the function:addNumber(100, function (number) { console.log(number); }); Is this an asynchronous code in Node.js? That is, does the function still runs in the event loop while blocking other operations? I am wondering because this function follows the usual "callback" pattern whenever we want to run asynchronous code.

Submitted May 21, 2018 at 09:39AM by VickNicks

No comments:

Post a Comment