By Ran Bar-Zik | 10/10/2018 | General |Beginners

ES2018 - Finally Method in Promise

ES2018 - Finally Method in Promise

You guys really don’t know just how hard it was for me not to name this post “Finally, the feature you’ve all been waiting for” or make some other kind of dad joke along those lines. In the end though, I decided to spare us all from it. In any case, today we’ll be talking about a really cool and important feature of Promises. If you’re not familiar with promises, it’s one of the essential features of the new JavaScript standards. You can get up to speed with this article that goes into more detail.

 

Finally is one of the more important additions that allows us to add a function that will always exist regardless of the results of the promise. So whether the promise is successful or not the finally will run. Let’s have a look at a code example:

 

const myPromise = new Promise((resolve, reject) => {

 setTimeout(() => { resolve('success!!!'); }, 2000);

});

This is a totally regular promise that has a resolve directly after 2000 milliseconds. If I want to work here, I’ll need to concatenate a then to it.  

 

myPromise.then(

 result => { console.log(result) },

 failMessage => { console.log(failMessage) }

);

This friendly little then has two anonymous functions. Thiefirst will run if there is a resolve and the second in cases of reject. This promise is always successful; we’ll always see ‘success’ in the console. Which is pretty nice. But I can add another finally that will always work:

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 the results of the promise, I’ll see success screaming at me in the console. Don’t believe me, oh ye of little faith? Go ahead and mess around with this:

And that? Well, that is pretty good stuff right there. And super useful. Why? Let’s say I have an animation for loading that starts as soon as I launch the promise. Here I can clean it. If I have some modal, it could be a great point to close it and so on. If I’m writing in Node.js, I can close connections. In short, it’s definitely a good one to get to know.

 

Previous article: ES2018 Unicode with RegEx

Next article: ES2019 flatMap

 

About the author: Ran Bar-Zik is an experienced web developer whose personal blog, Internet Israel, features articles and guides on Node.js, MongoDB, Git, SASS, jQuery, HTML 5, MySQL, and more. Translation of the original article by Aaron Raizen

By Ran Bar-Zik | 10/10/2018 | General

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Recent Stories

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
3220
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
3060
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All
X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now