1- //Generator
2- // a generator can be entered and exited multiple times. We can return a value and then go right back into it
3- //and keep going from the same location that we left.
4-
5- const colors = [ 'red' , 'green' , 'blue' ] ;
6-
7- //for/of loop
8- for ( let color of colors ) {
9- console . log ( color ) ;
10- }
11-
12- //the * makes this a generator
13- function * shopping ( ) {
14- //stuff on the sidewalk
15-
16- // walking down the sidewalk
17-
18- //go into the store with cash, //this is where we also reenter the generator
19- //with 'groceries' yield is a keyword that exits function and then when
20- //going back into the function we return to this place.
21- const stuffFromStore = yield 'cash' ;
22- //walking to laundrymat
23- const cleanClothes = yield 'laundry' ;
24- // walking back home
25- return [ stuffFromStore , cleanClothes ] ;
26- }
27-
28- //stuff in the store
29- //this doesn't actually go through the function
30- const gen = shopping ( ) ;
31- //gives {'done': false} leaving our house, this is where we actually go into
32- //the function.
33- gen . next ( ) ;
34- //walked into the store at this point
35- //walking through the store up and down the aisles
36- //purchased our stuff
37- // gives {'done': true} leaving the store with groceries
38- gen . next ( 'groceries' ) ;
39- gen . next ( 'clean clothes' ) ;
40-
41-
42- function * colors ( ) {
43- yield 'red' ;
44- yield 'green' ;
45- yield 'blue' ;
46- }
47-
48- const myColors = [ ] ;
49- for ( let color of colors ( ) ) {
50- myColors . push ( color ) ;
51- }
52-
53- myColors ;
54-
55-
56- const engineeringTeam = {
57- size : 3 ,
58- dept : 'Engineering' ,
59- lead : 'Jill' ,
60- manager : 'Alex' ,
61- engineer : 'Dave'
62- }
63-
64- function * TeamIterator ( team ) {
65- yield team . lead ;
66- yield team . manager ;
67- yield team . engineer ;
68- }
69-
70- const names = [ ] ;
71- for ( let name of TeamIterator ( engineeringTeam ) ) {
72- names . push ( name ) ;
73- }
74-
75- names ;
76-
77- //refractored
78- const testingTeam = {
79- lead : 'Amanda' ,
80- tester : 'Bill' ,
81- //the Symbol.iterator gets used if it exists to use the generator it is
82- //pointing at to iterate over the object. the Symbol.iterator just teaches
83- //the for/of loop how to iterate over the object. Note the use of this as
84- //we are referring to the object that this generator is within.
85- [ Symbol . iterator ] : function * ( ) {
86- yield this . lead ;
87- yield this . tester ;
88- }
89- }
90-
91- const engineeringTeam = {
92- size : 3 ,
93- dept : 'Engineering' ,
94- lead : 'Jill' ,
95- manager : 'Alex' ,
96- engineer : 'Dave' ,
97- //could just be testingTeam by itself since it's key value is same
98- testingTeam : testingTeam ,
99- [ Symbol . iterator ] : function * ( ) {
100- yield this . lead ;
101- yield this . manager ;
102- yield this . engineer ;
103- //this yield causes the for/of loop to iterate over the team.testingTeam
104- yield * this . testingTeam ;
105- }
106- }
107-
108- const names = [ ] ;
109- for ( let name of engineeringTeam ) {
110- names . push ( name ) ;
111- }
112- names ;
113-
114-
115- //tree
116-
117- class Comment {
118- constructor ( content , children ) {
119- this . content = content ;
120- this . children = children ;
121- }
122- * [ Symbol . iterator ] ( ) {
123- yield this . content ;
124- for ( let child of this . children ) {
125- yield * child ;
126- }
127- }
128- }
129-
130- const children = [
131- new Comment ( 'good comment' , [ ] ) ,
132- new Comment ( 'bad comment' , [ ] ) ,
133- new Comment ( 'meh' , [ ] )
134- ] ;
135-
136- const tree = new Comment ( 'Great post!' , children ) ;
137-
138- const values = [ ] ;
139-
140- for ( let value of tree ) {
141- values . push ( value ) ;
142- }
143-
144- values ;
1+ //promises
2+
3+ let promise = new Promise ( ( resolve , reject ) => {
4+ setTimeout ( ( ) => {
5+ resolve ( ) ;
6+ } , 3000 ) ;
7+
8+ } ) ;
9+
10+ promise
11+ . then ( ( ) => {
12+ console . log ( 'finally finished' ) ;
13+ } )
14+ . then ( ( ) => {
15+ console . log ( 'I was also ran!' )
16+ } )
17+ . catch ( ( ) => {
18+ console . log ( 'uh oh...rejected' ) ;
19+ } ) ;
20+
21+ //fetch
22+
23+ const url = 'https://jsonplaceholder.typicode.com/posts/' ;
24+
25+ fetch ( url )
26+ . then ( response => response . json ( ) )
27+ . then ( data => console . log ( data ) )
28+ //if fails, catch does not actually happen. unless the request flat out fails to happen
29+ . catch ( error => console . log ( 'Bad' , error ) ) ;
0 commit comments