Thank you for the invitation. The synchronization interfaces of node are for the convenience of developers and are provided natively.
Several other database interfaces are third-party modules implemented by other author organizations.
Under normal circumstances, it is recommended to write asynchronously. Of course, there will be various callbacks that are not elegant. At this time, it is recommended to use the various synchronization methods provided by the native to optimize the code and achieve elegance.
For example: Generator / Promise / Async and other native solutions.
Or mainstream asynchronous processing solutions encapsulated with co etc.
There is actually no difference in the process, but readFile is a native API, and the official provides this kind of synchronous call. And query is the api provided by the node-mysql module, which does not provide users with such methods.
Actually, you can implement one yourself. It just so happens that node v7+ can support async and await. Something like this:
Thank you for the invitation. The synchronization interfaces of node are for the convenience of developers and are provided natively.
Several other database interfaces are third-party modules implemented by other author organizations.
Under normal circumstances, it is recommended to write asynchronously. Of course, there will be various callbacks that are not elegant. At this time, it is recommended to use the various synchronization methods provided by the native to optimize the code and achieve elegance.
For example: Generator / Promise / Async and other native solutions.
Or mainstream asynchronous processing solutions encapsulated with co etc.
There is actually no difference in the process, but
readFile
is a native API, and the official provides this kind of synchronous call.And
query
is the api provided by the node-mysql module, which does not provide users with such methods.Actually, you can implement one yourself. It just so happens that node v7+ can support
async
andawait
.Something like this: