**What the `finally` block does**
```js
.finally(async () => {
await prisma.$disconnect()
})
```
`finally` is part of the promise chain that follows `main()`.
It runs **after** the promise returned by `main()` has either resolved (the user was created successfully) or rejected (an error was thrown).
Inside that block we call `prisma.$disconnect()`, which:
1. **Closes the Prisma client’s connection pool** – it tells Prisma to drop all open database connections.
2. **Returns a promise** that resolves when the disconnection is complete, and we `await` it so the cleanup finishes before the Node process exits.
In short, the `finally` block guarantees that the Prisma client is cleanly disconnected regardless of whether the script succeeded or failed.
---
**What would happen if the `finally` block were omitted**
- **Open connections remain** – The Prisma client would keep its database connections alive until the Node process terminates.
- **Potential process hang** – In a long‑running application (or when you run the script in a REPL or test runner), the process may not exit because the database pool is still active.
- **Resource leaks** – Each open connection consumes memory and a slot in the database’s connection limit. If you forget to disconnect, you could eventually hit the maximum number of connections or exhaust server resources.
In a very simple script that immediately ends, you might not notice a difference because the Node process exits when the script finishes. However, leaving the connection open is still bad practice and can cause subtle bugs in larger applications. Therefore, the `finally` block is used to guarantee proper cleanup and avoid hanging or leaking database connections.
mai mult












România
Disponibil și pe platforma B2B ›























