3

I am a java developer, who is right now trying to learn various concurrency models, currently looking at Node. As I understand following code does async io in node:-

  1.  fs = require('fs')
  2.  fs.readFile('/etc/hosts', 'utf8', function (err,data) {
  3.    if (err) {
  4.      return console.log(err);
  5.    }
  6.    console.log(data);
  7.  });

As I understand correctly, line-2, is an async call, telling node to read the file in parallel, and when done, execute callback function(appending it in the event queue?).

Does node provide any package where I can create a similar function for my own usecase, where I do an async function and place the callback for execution to event-loop?

I read that Node uses libuv package for implementing async behavior. Looks like there is a third party library on npm - npm-libuv_thread. Will it provide the mechanism I need?

Update: For the following code:-

var sleep = require('sleep');

printWithSleep();
console.log("Hello");

function printWithSleep(){
   sleep.sleep(5);
   console.log("World")
}

Node waits 5 seconds and then prints World followed by Hello, My question is - Is there any way I can make printWithSleep as async (currently its blocking)

Mangat Rai Modi
  • 5,397
  • 8
  • 45
  • 75
  • Also related - https://stackoverflow.com/questions/17204890/does-asynchronous-programming-in-node-js-speed-up-cpu-bound-tasks – Mangat Rai Modi Nov 24 '17 at 13:17
  • Accept a callback in your params, call it at the end, what else do you need ? Consider looking at [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), it's kind of the new callbacks. – Gabriel Bleu Nov 24 '17 at 14:07
  • @GabrielBleu It will vbe then executed in the same single thread of Node. If I am calling sleep for 2 secs, it will block whole application. Just like fs.read, I need a way so that this call goes to node's internal threadpool – Mangat Rai Modi Nov 24 '17 at 14:20
  • I more like need to know, how to implement my own fs.readFile() – Mangat Rai Modi Nov 24 '17 at 14:21
  • @MangatRaiModi your question is far too vague, what *exactly* is it you are looking to do? Node provides APIs like `readFile` so you don't have too, are you saying there is no native API to help you achieve what it is you want to do? – James Nov 24 '17 at 14:37
  • 1
    @James: he just wants to have a custom async method that behaves in a fashion **similar** to how `readFile` works. The question is very clear. – Wiktor Zychla Nov 24 '17 at 14:41
  • As for the OPs question: https://nodeaddons.com/building-an-asynchronous-c-addon-for-node-js-using-nan/ That's one of the ways. – Wiktor Zychla Nov 24 '17 at 14:46
  • @MangatRaiModi No it will not, thanks to event loop. Even with one single thread [see](https://stackoverflow.com/questions/17959663/why-is-node-js-single-threaded). If you want to go multithread, take a look at [cluster api](https://nodejs.org/api/cluster.html) – Gabriel Bleu Nov 24 '17 at 14:47
  • @WiktorZychla you're entitled to your opinion but for me it's not clear at all. A clear question explains exactly what they're trying to do e.g. *"I'm trying to read a file without blocking the thread"* not *"I am trying to write an async function that is similar to readFile"*; show some code, what have you tried? Vague questions incite guesswork - this is why you ask probing questions. If the OP was more precise then we can better answer the question for them. – James Nov 24 '17 at 15:33
  • James, I will add details by tomorrow. – Mangat Rai Modi Nov 24 '17 at 16:47

0 Answers0