📜 ⬆️ ⬇️

ES2018 - finally promis method

The author of the note, the translation of which we are publishing today, says that it was 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 the transition to async / await
Async / await: 6 reasons to forget about promises
Escape from hell async / await
JavaScript ES6: write less - do more
Promises guide for those who want to understand them
The construction of async / await in JavaScript: strengths, pitfalls and features of use
Use of promises in javascript
')

Promise.prototype.finally method


The ECMAScript 2018 clause Promise.prototype.finally devoted to the Promise.prototype.finally method . According to the caniuse.com resource, the support level of this method is approximately 81%. This method can also be used in the Node.js environment.

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

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

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

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

The then method is passed two anonymous functions. The first will be executed if the promise is successfully resolved. The second is when it is rejected. Our promise always completes successfully, the message success!!! will always be displayed 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? The finally method will help us here:

 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 promise completes the work, in the console, in addition to the corresponding message, the text FINALLY!! , which tells us that the callback function passed to the finally method works anyway. In order to verify this - you can experiment .

Results


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

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

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

Source: https://habr.com/ru/post/427405/


All Articles