paint-brush
Javascript Promises: Pinky’s Upby@SCDesigns
169 reads

Javascript Promises: Pinky’s Up

by Sean ClarkeSeptember 14th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

A <strong>promise</strong> is an object that produces a value in the future upon the completion of (more often than not) an api call, or some other supplied argument. At any given time, a promise is either either completed, rejected, or pending. Through callbacks we can utilize the completed value or provide feedback for what it may not have been provided.

Company Mentioned

Mention Thumbnail
featured image - Javascript Promises: Pinky’s Up
Sean Clarke HackerNoon profile picture

A promise is an object that produces a value in the future upon the completion of (more often than not) an api call, or some other supplied argument. At any given time, a promise is either either completed, rejected, or pending. Through callbacks we can utilize the completed value or provide feedback for what it may not have been provided.

For instance, in my most recent project, JetLog, I utilized promises extensively in handling and dealing with JSON data from my companion Rails API. As seen below, in my Redux Logs module, I make an asynchronous call to my API to fetch “logs”. Using .then(), I’m able to promise the function that something will be done with the data retrieved.

<a href="https://medium.com/media/8eedb6c2e3a6c879f1eab7e2d3bf6041/href">https://medium.com/media/8eedb6c2e3a6c879f1eab7e2d3bf6041/href</a>

In this case, I’m…

  1. fetching a response and ensuring it’s delivered as json
  2. taking the logs array and dispatching a callback of setlogs(logs), passing in the logs array [See Fig 2.].
  3. in the event there’s an error I deploy a .catch and alert of a Submission Error.
  4. If successful, the setlogs(logs) action will switch the reducer and update the logs state to reflect all the logs gathered from the call.

<a href="https://medium.com/media/d06428df74f3bda61e92edaa67364a12/href">https://medium.com/media/d06428df74f3bda61e92edaa67364a12/href</a>