X
X
xenonhammer2020-07-24 00:47:14
JavaScript
xenonhammer, 2020-07-24 00:47:14

What is the difference between asynchronous and synchronous method(nodejs javascript)?

I'm confused about some concepts. Since the javascript language is event-driven, everything is done synchronously, but to wait for the execution of, for example, a promise, we use the async / awayt syntactic sugar - i.e. do something asynchronously and then continue executing the script.
Therefore, something asynchronous suspends the execution of the rest of the script.
And here in nodejs:
fs.readFileSync
fs.readdirSync
The name is not Async, but Sync, and the documentation says that these are synchronous functions, but why do they also stop the execution of the rest of the script? If they are synchronous, I don't understand something?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2020-07-24
@xenonhammer

Therefore, something asynchronous suspends the execution of the rest of the script.
no. It was to avoid this that asynchronous was invented.
and the process is slowed down by blocking operations.
https://nodejs.org/ru/docs/guides/blocking-vs-non-...

K
kn1ght_t, 2020-07-24
@kn1ght_t

you need to read about the event loop
, the meaning is that the usual synchronous code is executed sequentially line by line in the main thread, and the asynchronous code is postponed to the task queue and executed after the synchronous code is executed.
and these fs.readFileSync and others contain the Sync postscript, as if hinting that these methods can be long (for example, you need to read a large file) and the rest of the code will wait for them, which is not good for server code, on the other hand it’s quite wonderful for service scripts when such a script is expected to be run by a single client

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question