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.