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

Javascript Promises: Pinky’s Up

by Sean Clarke
Sean Clarke HackerNoon profile picture

Sean Clarke

@SCDesigns

September 14th, 2017
Read on Terminal Reader
Read this story in a terminal
Print this story
Read this story w/o Javascript
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
Fetch
featured image - Javascript Promises: Pinky’s Up
Sean Clarke HackerNoon profile picture
Sean Clarke

Sean Clarke

@SCDesigns

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>

L O A D I N G
. . . comments & more!

About Author

Sean Clarke HackerNoon profile picture
Sean Clarke@SCDesigns

TOPICS

THIS ARTICLE WAS FEATURED IN...

Permanent on Arweave
Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite
Partlab
Aryan

Mentioned in this story

companies
X REMOVE AD