re: Finally in Promises & Try/Catch VIEW POST

FULL DISCUSSION
 

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');
  }
}
 
 

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.

code of conduct - report abuse