JavaScript promises

To Read more about Promises: MDN – Link1

This is an alternative system for managing Async-Programming in JavaScript, alternative because we have other ways to do this like Call-Backs. a really good explanation is in this videos: Link – Link2 Art1 – Art2

A promise is always in one of the following staties:

  1. Pending: Initial state, not fulfilled nor rejected.
  2. Fulfilled: Successful operation.
  3. Rejected: Failed operation.
  4. Settled: The promise is either: Fulfilled or Rejected, but not pending

Here is another graphic of the structure of the promise:

In the following example we use the: ” setTimeout() ” to create a situation where we can decide if we want to resolve or reject our promise, depending in which is executed firs, change the values of the time to see how the promise is resolved or rejected using the the methods: then or catch.

You can handle multiple promises at once and wait until all have been “resolve” before continue with the code. again change the times in “myProm1” to see how it resolves or executes.

An this is how promises work.

3 Dallas Cowboys Promise examples:

Example: 1

Your uncle has PROMISE you that if the Dallas Cowboys wins tonight’s game, he will meet you at IN & OUT for a hamburgers dinner is on him. and here is the code.

 

Example: 2

Next week he PROMISE that if the Dallas Cowboys win you he will pay for the Hamburgers again, and he make another promise that if the N.Y. Giants lose he will pay the Tickets for the Movies. but if one of the conditions is not meet both PROMISES are off the table, is like all or nothing.

 

Example: 3

Dallas vs Packers games results for the Playoffs.

.



Copyright 2017. All rights reserved.

Posted February 15, 2017 by Edmundo in category "JS