JavaScript Promises

my notes from the Udacity Course JavaScript Promises, by Cameron Pittman
https://classroom.udacity.com/courses/ud898

Notes

Creating Promises

Quotes

Every web developer needs to be able handle asynchronous work with confidence and ease

Definitions

  1. Promise Object - is used for deferred and asynchronous computation - MDN

  2. Asynchronous code - happens at and unknown or unpredictable time

  3. Fulfilled (Resolved) - It worked

  4. Rejected - It did not work

  5. Pending - Still waiting...

  6. Settled - Something happened!

  7. promise constructor -

new Promise(function(resolve, reject) {
  resolve('hello');  // works
  resolve('see ya'); // can not happen a second time
});
- a promise can only resolve once
- promises are not a way to safely run long running operations
- promises are way to do something when an asynchronous task settles (try catch wrappers around asynchronous work)

General Promise Lifecycle

  1. Wrapping
  2. Thening
  3. Catching
  4. Chaining

Wrapping Explanined

A promise is a try catch wrapper around code that will finish at an unpredictable time

Promise is a constructor (variable or work on it with out storing it)

You pass a function to the promise with two arguments:
- (resolve, reject])

Thening Explained

When the promise resolves, we do something. For example, document.readystate has three states:

  1. 'loading' - document loading
  2. 'interactive' - document loaded and parsed, but sub-resources have yet to resolve (images and stylesheets)
  3. 'complete' - all sunresources - images and stylesheets have loaded

Note: stopped this course because I could not get the examples to work...which made me loose interest

JavaScript Promises
Share this