JavaScript 6 for the C# Developer: Mini Series – Part 4: Promises

Before I begin, I should probably explain what a JavaScript Promise is. A promise allows you to associate a handle to an asynchronous event which will return either a success (resolve) or failure (reject). A promise is made so that the outward call will return in the future, attaching a then method to your JavaScript function will facilitate this future callback.

Much like my previous post in this series on OOP in JavaScript, promises are not really a new concept to the JavaScript world. There are many libraries out there already that help greatly in achieving this, such as the very common Q library. However, this is an external library that you have to include with your app. Due to its popularity, the ECMASCRIPT committee have taken note of the need for this feature and have baked it right into the new JavaScript. But it doesn’t stop there, what’s really exciting about JavaScript nowadays and with the browser movement towards being evergreen, is that the ECMASCRIPT committee will be working to deliver new versions of JavaScript on a yearly basis. That means that we may not have to wait as long as we did for version 6, with version 7 likely to be out next year! But what does this mean in relation to this post? Well, promises will be revamped again and made look even closer to their C# equivalent using the async/await pattern from the .NET 4.5+ framework.

So how do promises relate to anything in C# currently? C# implements a promise like syntax through the ContinueWith Task extension. This creates a continuation for the target task to return its result, whereby we can then do something with the result or check for an error.

Let’s see this in play, first let’s take a look at how we would represent a promise like implementation in C#:

Here we are simply creating a task. This task takes 3 seconds to complete and returns a successful message upon return to the PromiseExample method. The ContinueWith task extension will then take the result of the task and print it to the screen. So, how do we do the same in JavaScript 6?

We create a promise using the new Promise object and pass in a function that takes two parameters, resolve and reject. Within this method, depending on the outcome, we can simply call resolve which will return to the callers then extension method with whatever we pass back as its parameter. Or we can reject, passing back a reason. The reject works slightly differently in how it’s represented in JavaScript and C# which is why I haven’t put it in this example. In C# you handle the error state by checking if the returned call IsFaulted or IsCanceled. In JavaScript 6, you can chain the catch method following the then method and process the response, much like how you would in the then method above. Another similarity to the then and ContinueWith methods, is that you can chain multiple then methods one after the other, just like you can with the ContinueWith.

Well that’s it for another addition to the JavaScript 6 for the C# Developer series.


Thanks for reading and keep building awesome.

Until next time!

Ian B

P.S. You can also find me on Twitter: @ianaldo21

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s