node.js synchronous mysql query [closed]

巧了我就是萌 提交于 2019-12-13 22:24:37

问题


How can I use mysql synchronously in node.js ? I want to execute query and render properly rendered form with datas from database, like:

userData = db.query "SELECT * FROM users WHERE id=1"
form.bind(userData)
res.render 'user/edit', {'form' : form}

回答1:


By the title I assume that you mean in a synchronous way. The answer is you can't. This is because NodeJS is single-threaded and since I/O (i.e. communication with database) is handled in a separate thread (which is used internally by NodeJS, it is not available for programmers) the result of I/O is pushed to the top of the event loop. But NodeJS can't jump to another event in the event loop without leaving current event.

Whether you like it or not you have to stick with asynchronous patterns.



来源:https://stackoverflow.com/questions/15902724/node-js-synchronous-mysql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!