The JavaScript APIs based on Promises

The new asynchronous JavaScript APIs are now based on the Promise pattern instead of events and callbacks. And the new versions of the old JavaScript APIs are now based on Promises.

For example, the old version of the Battery status API and the Web Cryptography API were based on event, but the new versions of these APIs are purely implemented using Promises. Let's see an overview of these APIs.

The Battery Status API

The Battery Status API provides us the battery's current charge level and charging status. Here is a code example of the new Battery Status API:

navigator.getBattery().then(function(value){ console.log("Batter Level: " + (value.level * 100)); }, function(reason){ console.log("Error: " + reason); }); ...

Get React: Building Modern Web Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.