MySQL Forums
Forum List  »  Connector/Node.js

Re: Is there any npm library available for mysql which is synchronous?
Posted by: Rui Quelhas
Date: September 19, 2022 02:06PM

Hi Prithwiraj,

I will simplify the explanation a bit because it is mostly off-topic for this specific forum.

Due to the single-threaded non-blocking programming model used most often by Node.js applications, it does not make too much sense to implement I/O-bound operations such as database access using synchronous code, because it would block the single main thread that runs the application.

There are some multi-threaded constructs for JavaScript and Node.js in particular, but those are fairly new and are not widely used.

So, usually, for most I/O-bound APIs in general, and database drivers in particular, you usually need (and want) to use some asynchronous pattern such as callback functions, Promises or Generators.

When you use a Promise-based API, you can leverage async/await syntatic sugar, which allows the code to flow in a serial fashion, similar to what it would look like in a synchronous API.

Using your example:

const mysqllib = require('mysqllib');

const connectToDatabase = async () => {
  const conn = await mysqllib.connect(...);
  const res = await conn.query(...);

  // ...

Hope it helps.

Options: ReplyQuote

Written By
Re: Is there any npm library available for mysql which is synchronous?
September 19, 2022 02:06PM

Sorry, you can't reply to this topic. It has been closed.
This forum is currently read only. You can not log in or make any changes. This is a temporary situation.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.