Câu hỏi phỏng vấn Nodejs
Câu hỏi

What is a Blocking Code in Node.js?

Câu trả lời

In Node.js, a blocking code refers to operations that halt the execution of additional JavaScript until a non-JavaScript operation completes. This type of operation is synchronous, meaning the Node.js event loop is unable to continue running other JavaScript tasks until the blocking operation has finished[2][8][11].

Blocking behavior typically involves I/O operations, such as reading from or writing to a file system, network requests, or any other operations that require waiting for external processes to complete. When these operations are executed synchronously, the system must wait for the operation to finish before moving on to the next line of code. This can lead to inefficiencies, especially in a Node.js environment, which is designed to handle high concurrency through non-blocking and asynchronous operations[2][8].

For example, using the readFileSync method from the Node.js fs (File System) module to read a file is a blocking operation. The method will block the rest of the code from executing until the entire file is read:

javascript Copy
const fs = require('fs');
const data = fs.readFileSync('/path/to/file');
console.log(data);
console.log('This will not run until the file is read');

In this code, the console.log('This will not run until the file is read'); line will not execute until the file reading...

middle

middle

Gợi ý câu hỏi phỏng vấn

senior

Explain what is Arrange-Act-Assert pattern?

senior

Is an Event Emitter synchronous or asynchronous?

expert

Explain what is Reactor Pattern in Node.js?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào