How does it work asynchronously under the hood ..?

I studied a lot of multithreading, callback, send queue, synchronously and asynchronously ... The more I do the research, the more embarrassed and frustrated I am to the extent that I feel like I can’t understand what it is. Please someone can lead me in the right direction to start ... the information that I have found so far has been related to what is and what is connected with it. What I really want to know is that the function returns immediately on an asynchronous callback and in the same thread. [here] ( http://nathansjslessons.appspot.com/lesson?id=1085 ) that I received this information from

The function **returns immediately** before the file is read and schedules the read to happen       
sometime in the future. Once the data is ready, the callback function is called on the    
data.

Here is an example of how to use the regular lock read function to get the contents of a file   var readFile = function () {

var data;
data = read('file.txt');
dosomething('contect' + data);
}

The same example uses the asynchronous readAsync function.

var readFileAsynch = function () {
     var func = function (x) {
          // i can do something with data
         dosomthing('content'+data);
     }
     **readAsynch('file.txt',func);** 
      dosomemorestuff();
     };

from what I know, if you use a different thread than the main thread, than I thought the way to do asynchronous, then if you have only one thread , for example javascript, then how would it work asynchronously.?

And when it comes to send queues in object c, is it right to think that a queue is just an array of pointers to blocks or functions, and the thread is responsible for managing this queue in the application ..?

, , .. , -, , , , . , - , " "... ..? , ..?

edit: readAsynch ('file.txt', func); , , asynch..? , dosomemorestuff , readAsynch , ( ), readAsynch, .?

+4
3

, , , , .

, , :

JavaScript "" , , , , . JavaScript, "" .

, , . , 3 , , .

, , , node.js, :

, , 2 . , , . , , 2 , 2 , - 4 . , , , , .

Async : - (DOM, ..) , , , , , , ' , . , mouseclick keypress . PAINFULL, , , .

, , .

+2

, . " ". , , - . , , . , .

- , . , , , . ( , , , ). , , , .

, . , . . , , , , .

+2

, , async ( Node.js) async .

Event Loop process.nextTick(), , Node.js.

:

let bar;

function someAsyncApiCall(callback) {
  process.nextTick(callback);
}

someAsyncApiCall(() => {
  console.log('bar', bar); // 1
});

bar = 1;

, , process.nextTick(), script , , .. .

Here is another example:

// defining an asynchronous function
var async_function = function(callback){
    console.log("Executing code inside the async function");
    process.nextTick(function(){
      console.log("Executing code in callback provided to process.nextTick() inside async function");

      const startCallback = Date.now();
      while (Date.now() - startCallback < 2000) {
        // do nothing
      }
      callback();
    });
};

console.log("Code executed prior to calling the async function");
// calling the async function
async_function(function() {
    console.log("Executing the callback function provided to async function");
});
console.log("Code executed after calling the async function");

// Output:

// Code executed prior to calling the async function
// Executing code inside the async function
// Code executed after calling the async function
// Executing code in callback provided to process.nextTick() inside async function
// (pause of 2 seconds)
// Executing the callback function provided to async function
+1
source

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


All Articles