Skip to main content
All CollectionsSecurity Journey FAQs
Why we use the mysql2/promise library for javascript and typescript
Why we use the mysql2/promise library for javascript and typescript
John Campbell avatar
Written by John Campbell
Updated over a week ago

tldr: it's just a promise wrapper around the mysql2 library.

We prefer using the mysql2/promise library over the mysql2 library because of the streamlined and modern interface it provides through promises. While it's true that the mysql2 library has more downloads and is widely used, the mysql2/promise library offers a more convenient and efficient way to handle asynchronous operations.

The key reasons for our preference include:

1. Promise-Based API: The mysql2/promise library is essentially the same as the mysql2 library but adds native promise support. This allows us to use async/await syntax, making our code cleaner, more readable, and easier to manage.

2. Error Handling: Using promises simplifies error handling. Instead of dealing with nested callbacks, we can handle errors using try/catch blocks, which aligns with modern JavaScript practices.

3. Code Clarity: With the promise interface, we can avoid callback hell and write more linear and understandable code. This is particularly beneficial for maintaining and scaling our application.

4. Consistency: Since many modern JavaScript libraries and frameworks are moving towards promise-based APIs, using mysql2/promise helps us maintain consistency across our codebase.

While the mysql2 library is robust and popular, the mysql2/promise library provides all the same functionality with the added benefit of promises, making it a better fit for our lessons.

Did this answer your question?