Javascript Promises: Pinky’s Up

Written by SCDesigns | Published 2017/09/14
Tech Story Tags: javascript

TLDRvia the TL;DR App

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>


Published by HackerNoon on 2017/09/14