ES2018 - finally promis method

Original author: Ran Bar-Zik
  • Transfer
The author of the note, the translation of which we are publishing today, says that it is difficult to refrain from calling it “Finally - an opportunity that everyone was waiting for”, or somehow joke about this topic. In the end, he decided to do without jokes and just talk about the really important and useful features of Promise objects.


If you are just starting to learn JavaScript and are not very familiar with promises (sometimes they are called “promises”, “promised results”, “Promise objects”), then you may be interested in our previous publications on this topic:

Promises in ES6: patterns and anti-patterns
JavaScript: asynchronous programming methods
JavaScript ES8 and transition to async / await
Async / await: 6 reasons to forget about promises
Escape from hell async / await
JavaScript ES6: write less - do more
Guide promises for those who want to understand them
Construction of async / await in JavaScript: strengths, pitfalls, and features of use
Using promises in JavaScript

Method Promise.prototype.finally


A method Promise.prototype.finallyis dedicated to the item 25.6.5.3 of the standard ECMAScript 2018. According to the information resource caniuse.com , the level of support for this method is approximately 81%. This method can also be used in the Node.js environment.

The promis method finallyis one of the most important innovations of the standard, which allows you to set a function that will be performed regardless of the result of promis. Such a function will be performed with the successful resolution of promis and with its rejection.
Consider an example:

const myPromise = newPromise((resolve, reject) => {
 setTimeout(() => { resolve('success!!!'); }, 2000);
});

This is a completely common promise that resolves after 2000 milliseconds. If after this you need to perform some kind of action, we will need a block then:

myPromise.then(
 result => { console.log(result) },
 failMessage => { console.log(failMessage) }
);

thenTwo anonymous functions are passed to the method . The first will be executed if the promise is successfully resolved. The second is when it is rejected. Our promise always finishes successfully, a message will always be displayed in the console success!!!. All this is very good, but what if you need to, so that certain actions would be performed after the rejection of the promise, and after the successful completion of its work? Here the method will help us finally:

const myPromise = new Promise((resolve, reject) => {
 setTimeout(() => { resolve('success!!!'); }, 2000);
});
myPromise.then(
 result => { console.log(result) },
 failMessage => { console.log(failMessage) }
).finally(finallyMessage => { console.log('FINALLY!!')});

Regardless of how the promise finishes, the console, in addition to the corresponding message, will display text FINALLY!!, which tells us that the callback function passed to the method finallyworks in any case. In order to verify this - you can experiment .

Results


The fact that the method appeared in ES2018 Promise.prototype.finallysuggests that in the foreseeable future a very high level of browser support can be expected. This means that what previously had to use support tools created by third-party developers, can now be implemented using standard tools.

In what situations can the method be useful Promise.prototype.finally? For example - if at the launch of the promise used to load something, some animation starts to play, finallyyou can complete this animation. In a block, finallyyou can, for example, close a certain modal window. In fact, there are many situations in which a method finallycan be useful.

Dear readers! Have you used substitutes for the method of finally promises before its standard implementations appeared?


Also popular now: