javascriptpromisefinally

Why would the loading indicator stay active if the promise was settled?


I was reading this article about Promise Basics on Javascript.info and came across an example about a usecase for the .finally() method.

It says:

The idea of finally is to set up a handler for performing cleanup/finalizing after the previous operations are complete.

E.g. stopping loading indicators, closing no longer needed connections, etc.

I'm not sure about the implementation of a loading indicator, but I assume this example doesn't jump to conclusions.

So assuming that I have some loading indicator that waits for some promise to settle, then it gets settled. Why would the loading indicator stay active if the promise was settled then?

It's just an abstract question.


Solution

  • The hypothetical loading indicator it discusses is one controlled by the author's code.

    const loading = document.createElement('img');
    loading.src="loading.gif";
    loading.alt="Loading!";
    
    document.body.append(loading);
    do_something().finally(() => loading.remove());
    

    It wouldn't disappear without you writing code to make it disappear because JavaScript does only what you tell it to do.