About asynchronous methods and threads

What is really going on behind the scenes with asynchronous functions?

Does it open a new thread and allow the OS to start and run it?

If so, could this cause deadlocks or other thread problems?

Here is an example of an asynchronous method:

var fs = require('fs')
var file = process.argv[2]

fs.readFile(file, function (err, contents) {
  var lines = contents.toString().split('\n').length - 1
  console.log(lines)
})
0
source share
1 answer

In fs.readFile(file,callback). This is a non-blocking call that means.

  • The node stores the main thread callbackin the event table and associate it with the event that will be emitted whenever the read process is executed.
  • node ( ) node .
  • ( ).
  • , , , callback task-queue, ().
  • () , callback .

.

, , , Deadlock . , callback

+1

Source: https://habr.com/ru/post/1652977/


All Articles