Photo by Max Chen on Unsplash

Javascript essentials: What is A Promise?

Before talking about promises, we should talk about “producing code” and “consuming code”. Producing code can be described as code that takes time, an example of this would be code that loads data on a network. Consuming code waits for the results of the producing code whenever it is ready. A Promise is something that links these two using a JavaScript object. What the promise does, is it allows the “producing code” to take all the time it needs to load, and offers the results of that promise to the subscribed code when it is ready.

A promise in JavaScript is not unlike a promise in our ever day life. A promise is an object that can offer you one of 3 things, when it is fulfilled it returns an object with a result value, when it is pending it means that object is undefined, and when it is rejected it means you’ll get an error object. Promises also come with guarantees on how your code will run. Callbacks will at no time be called before the current run of the JavaScript event loop. If a callback is called with a “.then()” it will be created regardless of the success or failure of the asynchronous operation. Multiple callbacks can be created with the “.then()” and each one will be executed one after the other in the order that you ordered your code.

Rules For Promises

A standard for promises was defined by the Promises/A+ specification community, highly recommend this resource if you want to learn more about promises and even implementing it in other languages like python, ActionScript, and Java.

JavaScript promises follow these specific set of rules:

  • A promise or “thenable” is an object that supplies a standard-compliant .then() method.
  • A pending promise may transition into a fulfilled or rejected state.
  • A fulfilled or rejected promise is settled, and must not transition into any other state.
  • Once a promise is settled, it must have a value (which may be undefined). That value must not change.

In ES6 promise constructors take in a function. The function can be one of 2 things, resolve(which gives us a value) and rejected(which gives us an error).

The “.catch()” in the end is used for when the promise is rejected and we receive an error message. Both the .then() and the .catch() methods will avoid the other if resolved or rejected is true. Meaning resolved promises will miss the .catch() and rejected promises will miss any of the .then().

Promises are basic JavaScript objects that join the producing and the consuming code. Promises allow us to create a natural flow to our code, and create an order of how we want things to be run. We can call .then as many times as we want, adding new callback opportunities and chaining promises together. .Catch is the rejected side of the .then, making sure that whatever rejected value there is, is still presented. Some great Promises resources include :

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store