tezvyn:

Promise Cleanup with .finally()

Source: developer.mozilla.orgadvanced

Promise.finally() is the `try...catch...finally` for async code, guaranteeing logic runs after a promise settles. Use it to hide a loading spinner or close a network connection without duplicating code in `.then()` and `.catch()`.

Promise.finally() acts like the `finally` block in a `try...catch` statement, but for async operations. It schedules a function to run once a promise is settled, whether fulfilled or rejected. This is ideal for cleanup actions like turning off a loading indicator or closing a database connection, avoiding duplicate logic in both `.then()` and `.catch()`. The key footgun: the callback receives no arguments and its return value is ignored, preserving the original promise's outcome.

Read the original → developer.mozilla.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

Promise Cleanup with .finally() · Tezvyn