Make the most of 'promises' in JavaScript

Handling concurrency in distributed applications is a challenge. Here's how to use promises and keep everything on track

Make the most of promises in JavaScript
Pixabay (Creative Commons BY or BY-SA)

Once we coded applications that stood alone or were tightly coupled to specific servers. Now we’re writing applications that are loosely coupled, with app and browser front ends connected to distributed cloud services. This simplifies many things, but also causes new problems.

The biggest problem facing the modern application developer is one of the oldest in distributed computing: how to ensure consistency among the various components of an application. This question goes to the heart of delivering modern applications -- especially those made up of loosely coupled microservices.

JavaScript is a key technology in this new world, so it’s no surprise that in the next major revision of the underlying ECMAScript standard, features will be added to help handle consistency -- features that are already appearing in the latest browsers. At the heart of JavaScript’s asynchronous connection-handling are “promises,” which provide a new way of scheduling how your code will work with JavaScript APIs and functions.

The promise of promises

Promises are ECMAScript 6 objects that wrap specific actions, so they always either complete or are rejected.

Promise-like features in various JavaScript libraries have emerged previously to give JavaScript applications support for asynchronous connections. Some, such as RSVP.js and JQuery’s Deferreds, have been quite popular. Now that JavaScript has native support for promises, however, you can avoid hefty library downloads and use the APIs built into your users’ browsers. Of all the major browsers, only the legacy IE fails to handle promises (Windows 10’s Edge browser does handle them).

To continue reading this article register now

How to choose a low-code development platform