DEV Community

Discussion on: Finally in Promises & Try/Catch

Collapse
 
frankrobert profile image
Frank Robert

Great write-up! I've only ever found a single good use for finally. You could do performance checks using console.time.

// logs async evaluation in ms
const promises = [new Promise(), new Promise()];

for (const promise of promises) {
  console.time('timer');
  try {
    await promise();
  } catch(error) {
    console.error(error);
  } finally {
    console.timeEnd('timer');
  }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
functionalstoic profile image
JasonSooter

Nice tip!

Collapse
 
mcstepp profile image
Meg Stepp

I also use finally to toggle loading indicators to keep things DRY in my then/catches. If I'm doing the same line of code in both, I move it to the finally block.