Node.js is stated to be asynchronous, event-driven, non-blocking I/O, but how can I identify if my script is asynchronous? Or non-blocking?
Node.js is stated to be asynchronous, event-driven, non-blocking I/O, but how can I identify
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is a list of functions which are asynchronous, including:
From File System module:
many more for
fs, basically asynchronous have their synchronous versions with “Sync” suffix at the end ( synchronous versions should be avoided though ).There are many many more examples. EventEmitters can be considered asynchronous ( for example HTTP servers or streams ), although technically they aren’t ( they depend on asynchronous operations ).
Basically almost everything which accepts
callbackas a parameter is asynchronous ( or depends on asynchronous operation ), for example:Keep in mind that this is obviously not a rule, notable example:
So synchronous code is a code which fires line after a line and order of lines matters. The asynchronous code is the opposite: the order of lines ( almost ) does not matter. In the example with
process.nextTickyou can change the order ofprocess.nextTickandconsole.log('test')and the result will still be the same.Read the documentation and try to memorize all basic asynchronous functions.
And one more advice: the best way to learn is learn by doing. 😉