Slide 1

Slide 1 text

by Jennifer Bland ES6 Promises & Generators

Slide 2

Slide 2 text

2 Who am I? Senior So/ware Engineer Hearst Autos h8p://www.jenniferbland.com h8ps://github.com/ratracegrad/ [email protected] @ratracegrad

Slide 3

Slide 3 text

3 Promises

Slide 4

Slide 4 text

4 Background “It all started with the callbacks” It was the best of Kmes. It was the worst of Kmes.

Slide 5

Slide 5 text

5 What is a callback? Callback is a way of telling your code to execute this code when this other piece of code completes.

Slide 6

Slide 6 text

6 JavaScript & Asynch Code •JavaScript event loop is single threaded. •Asynch code immediate return •Code executes in another loop •When finish event loop returns callback

Slide 7

Slide 7 text

7 Callback Example

Slide 8

Slide 8 text

8 Challenges with Callbacks •Easy to create callback hell •Error handling is easy to miss •Difficult to understand

Slide 9

Slide 9 text

9 Promises Background •Prior to ES6 we had Promise libraries like Bluebird & q •Promises are naKve in ES6 •Based off the Promises/A+ open standard

Slide 10

Slide 10 text

10 What is a Promise? A Promise represents the eventual result of some type of operaKon which may be available now, or in the future, or never. Provides same funcKonality as callbacks but are more powerful because they are composable.

Slide 11

Slide 11 text

11 Lifecycle of a Promise? 1. Promise object is created 2. Code inside promise runs 3. Promise resolves or rejects 4. Promise emits change event

Slide 12

Slide 12 text

12 Promises have states 1. Pending - have not fulfilled or rejected the Promise 2. Fulfilled - promise succeeded 3. Rejected - promise failed 4. Se8led - fulfilled or rejected

Slide 13

Slide 13 text

13 CreaKng a Promise 1. In ES6 a Promise is a constructor 2. Inside Promise we supply what is called the
 executor which is run when Promise is instanKated 3. Executor takes two arguments - resolve and reject 4. Promise calls its resolve funcKon when it succeeds
 and reject funcKon otherwise

Slide 14

Slide 14 text

14 Promise Example

Slide 15

Slide 15 text

15 Chaining Promises A Promise is an Object. 
 Promise Prototype has .then() and .catch() methods
 These methods return promises
 You can chain promises

Slide 16

Slide 16 text

16 Chaining Promise Example

Slide 17

Slide 17 text

17 Error Handling If a Promise fails then its reject() funcKon is called
 or the .catch()

Slide 18

Slide 18 text

18 Error Handling Example

Slide 19

Slide 19 text

19 Promise.all 1. Has an iterable as an argument 2. Returns a promise that resolves when all the
 promises in the iterable argument have
 resolved, or rejects with the reason of the first
 promise that rejects.

Slide 20

Slide 20 text

20 Promise.all Example

Slide 21

Slide 21 text

21 Promise.race 1. Has an iterable as an argument 2. Returns a promise that resolves or rejects as
 soon as one of the promises in the iterable
 resolves or rejects.

Slide 22

Slide 22 text

22 Promise.race Example

Slide 23

Slide 23 text

23

Slide 24

Slide 24 text

24 Generators

Slide 25

Slide 25 text

25 Background •FuncKons run to compleKon •FuncKons receive data once •FuncKons return data once

Slide 26

Slide 26 text

26 What is a Generator? •A generator looks like a funcKon but behaves like
 an iterator. •It pauses it’s execuKon between calls. •It generates many values over Kme by returning 
 an object which can be iterated over.

Slide 27

Slide 27 text

27 CreaKng a Generator 1. Generator funcKon starts with funcKon* 2. Generator funcKon creates an iterator which is
 executed with iterator.next() method is called 3. Inside a Generator funcKon, have yield keyword
 which suspends execuKon of Generator 4. Generator can yield any number of Kmes by 
 calling the next() method 5. The done properly indicates whether or not the
 Generator has yielded its last value

Slide 28

Slide 28 text

28 Generator Example

Slide 29

Slide 29 text

29

Slide 30

Slide 30 text

30 Tweet •Please tweet about presentaKon •Use #devnexus and #wwcatl

Slide 31

Slide 31 text

31 We Are Hiring! •More than 12 MM unique visitors per month •More than 100MM pages viewed per month •More than 2MM followers on Facebook Tech Stack is Node, Express, Angular 2, Docker Benefits of working for a startup but financed by one of the largest media publishing companies in the world.

Slide 32

Slide 32 text

32 h8ps://goo.gl/mdlkkP

Slide 33

Slide 33 text

33 h8ps://goo.gl/mdlkkP h8p://www.jenniferbland.com h8ps://github.com/ratracegrad/ [email protected] @ratracegrad