Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
610 views
in Technique[技术] by (71.8m points)

javascript - 同步编程和异步编程之间有什么区别(在node.js中)(What is the difference between synchronous and asynchronous programming (in node.js))

I've been reading nodebeginner And I came across the following two pieces of code.(我一直在阅读nodebeginner并且遇到了以下两段代码。)

The first one:(第一个:) var result = database.query("SELECT * FROM hugetable"); console.log("Hello World"); The second one:(第二个:) database.query("SELECT * FROM hugetable", function(rows) { var result = rows; }); console.log("Hello World"); I get what they're supposed to do, they query the database to retrieve the answer to the query.(我得到了他们应该做的事情,他们查询数据库以检索查询的答案。) And then console.log('Hello world') .(然后console.log('Hello world') 。) The first one is supposedly synchronous code.(第一个应该是同步代码。) And the second one is asynchronous code.(第二个是异步代码。) The difference between the two pieces is very vague to me.(这两部分之间的区别对我来说很模糊。) What would the output be?(输出是什么?) Googling on asynchronous programming didn't help me either.(搜寻异步编程也没有帮助我。)   ask by Azeirah translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The difference is that in the first example , the program will block in the first line.(区别在于,在第一个示例中 ,程序将在第一行中阻塞。)

The next line ( console.log ) will have to wait.(下一行( console.log )必须等待。) In the second example , the console.log will be executed WHILE the query is being processed.(在第二个示例中 ,将在处理查询时执行console.log 。) That is, the query will be processed in the background, while your program is doing other things, and once the query data is ready, you will do whatever you want with it.(也就是说,查询将在后台处理,而您的程序正在执行其他操作,并且一旦查询数据准备就绪,您就可以使用它进行任何操作。) So, in a nutshell: The first example will block, while the second won't.(简而言之:第一个示例将阻止,而第二个示例则不会。) The output of the following two examples:(以下两个示例的输出:) // Example 1 - Synchronous (blocks) var result = database.query("SELECT * FROM hugetable"); console.log("Query finished"); console.log("Next line"); // Example 2 - Asynchronous (doesn't block) database.query("SELECT * FROM hugetable", function(result) { console.log("Query finished"); }); console.log("Next line"); Would be:(将会:) Query finished
Next line Next line
Query finished Note(注意)
While Node itself is single threaded , there are some task that can run in parallel.(尽管Node本身是单线程的 ,但有些任务可以并行运行。) For example, File System operations occur in a different process.(例如,文件系统操作在不同的过程中发生。) That's why Node can do async operations: one thread is doing file system operations, while the main Node thread keeps executing your javascript code.(这就是为什么Node可以执行异步操作的原因:一个线程在执行文件系统操作,而Node的主线程一直在执行您的JavaScript代码。) In an event-driven server like Node, the file system thread notifies the main Node thread of certain events such as completion, failure, or progress, along with any data associated with that event (such as the result of a database query or an error message) and the main Node thread decides what to do with that data.(在节点等事件驱动服务器中,文件系统线程将某些事件(例如完成,失败或进度)以及与该事件关联的任何数据(例如数据库查询或错误的结果)通知主节点线程。消息),然后由主节点线程决定如何处理该数据。) You can read more about this here: How the single threaded non blocking IO model works in Node.js(您可以在此处了解更多信息: 单线程无阻塞IO模型如何在Node.js中工作)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...