es7 es7
play

ES7 ES7 The Evolution of JavaScript Who is Jafar Husain? - PowerPoint PPT Presentation

ES7 ES7 The Evolution of JavaScript Who is Jafar Husain? Architect of Falcor, Netflixs OSS data platform Netflix representative TC-39 Formerly worked for MS and GE ES6 spec will be finalized in June 2015 Isnt it too early to


  1. Retrieving function* results function * ¡getNumbers() ¡{ ¡ ¡ ¡ ¡ ¡yield ¡42; ¡ ¡ ¡ ¡ ¡yield ¡32; ¡ ¡ ¡ ¡ ¡ return ¡19; ¡ } ¡ > ¡var ¡iterator ¡= ¡getNumbers(); ¡ > ¡console.log(iterator. next ()); ¡ ¡ > ¡{ ¡value: ¡42, ¡done: ¡false ¡} ¡ > ¡console.log(iterator. next ()); ¡ ¡ > ¡ ¡ > ¡{ ¡value: ¡32, ¡done: ¡false ¡} ¡ > ¡ > ¡console.log(iterator. next ()); ¡ ¡ > ¡{ ¡value: ¡19, ¡ done : ¡true ¡} ¡ > ¡

  2. Iteration Consumer Producer 42 ¡ var ¡iterator ¡= ¡producer. iterator (); ¡ var ¡result ¡= ¡iterator. next () ¡

  3. Iteration Consumer Producer 39 ¡ var ¡result ¡= ¡iterator. next () ¡

  4. Iteration Consumer Producer done ¡ var ¡result ¡= ¡iterator. next () ¡

  5. How Y How Yield W ield Works orks function* ¡getFibonnacciSequence() ¡{ ¡ function ¡getFibonnacciSequence() ¡{ ¡ function ¡getFibonnacciSequence() ¡{ ¡ function ¡getFibonnacciSequence() ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡{ ¡ ¡ ¡ ¡ ¡return ¡{ ¡ ¡ ¡ ¡ ¡return ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡next: ¡function() ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡next: ¡function() ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡next: ¡function() ¡{ ¡ ¡ ¡ ¡ ¡let ¡val1 ¡= ¡0, ¡val2 ¡= ¡1, ¡swap; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡let ¡val1 ¡= ¡0, ¡val2 ¡= ¡1, ¡swap, ¡state ¡= ¡0; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡let ¡val1 ¡= ¡0, ¡val2 ¡= ¡1, ¡swap, ¡state ¡= ¡0; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡let ¡val1 ¡= ¡0, ¡val2 ¡= ¡1, ¡swap, ¡ state ¡= ¡0 ; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡switch(state) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡switch(state) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ switch(state) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡case ¡0: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡case ¡0: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡case ¡0: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡state ¡= ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡state ¡= ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡state ¡= ¡1; ¡ ¡ ¡ ¡ ¡ yield ¡val1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡{value: ¡val1, ¡done: ¡false}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ return ¡{value: ¡val1, ¡done: ¡false}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡{value: ¡val1, ¡done: ¡false}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡case ¡1: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡case ¡1: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡case ¡1: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡state ¡= ¡2; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡state ¡= ¡2; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡state ¡= ¡2; ¡ ¡ ¡ ¡ ¡ yield ¡val2; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡{value: ¡val2, ¡done: ¡false}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡{value: ¡val2, ¡done: ¡false}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡{value: ¡val2, ¡done: ¡false}; ¡ ¡ ¡ ¡ ¡while(true) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡default: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡default: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡default: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡swap ¡= ¡val1 ¡+ ¡val2; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡swap ¡= ¡val1 ¡+ ¡val2; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡swap ¡= ¡val1 ¡+ ¡val2; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡swap ¡= ¡val1 ¡+ ¡val2; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡val2 ¡= ¡swap; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡val2 ¡= ¡swap; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡val2 ¡= ¡swap; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡val2 ¡= ¡swap; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡val1 ¡= ¡val2; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡val1 ¡= ¡val2; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡val1 ¡= ¡val2; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡val1 ¡= ¡val2; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ yield ¡swap; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡{value: ¡swap, ¡done: ¡false}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡{value: ¡swap, ¡done: ¡false}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡{value: ¡swap, ¡done: ¡false}; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ } ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡} ¡ } ¡ } ¡ } ¡

  6. All ES6 collections are Iterable . ES6 ES6

  7. Iterator Pattern > ¡var ¡iterator ¡= ¡[42, ¡39] [@@iterator] (); ¡ > ¡console.log(iterator. next ()); ¡ ¡ > ¡{ ¡value: ¡42, ¡done: ¡false ¡} ¡ > ¡ > ¡console.log(iterator. next ()); ¡ ¡ ¡ > ¡{ ¡value: ¡39, ¡done: ¡false ¡} ¡ > ¡console.log(iterator. next ()); ¡ ¡ > ¡ > ¡{ ¡ done : ¡true ¡} ¡ > ¡

  8. Consuming Consuming Iterables with for for.. ..of of > ¡ for (var ¡x ¡ of ¡[42,39,17]) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡console.log(x); ¡ ¡ ¡} ¡ ¡ > ¡42 ¡ ¡ > ¡39 ¡ > ¡17 ¡ > ¡

  9. If generator functions return iterators…

  10. …why aren’t they called Iterator functions? ? ?

  11. Generator Iterator Observer

  12. A Generator is an Iterator yield value generator.next(). value ; ¡ throw an error try ¡{ ¡ generator.next(); ¡ } ¡ catch(err) ¡{ ¡... ¡} ¡ return value ¡ var ¡pair ¡= ¡generator.next(); ¡ if ¡(pair. done ) ¡alert(pair.value); ¡ pull

  13. A Generator is an Observer receive data generator.next( 5 ); ¡ receive an error generator. throw (“fail”); ¡ receive return value ¡ generator. return (5); ¡ push

  14. Iteration only allows data to flow one-way. done ¡ 96 ¡ 23 ¡

  15. Generators allow feedback. 96 ¡ 33 ¡ done ¡ 21 ¡ 99 ¡

  16. Waiting with Async Iteration function * ¡getStockPrice(name) ¡{ ¡ ¡ ¡ ¡ ¡var ¡symbol ¡= ¡ yield ¡ getStockSymbol(name); ¡ ¡ ¡ ¡ ¡var ¡price ¡= ¡ yield ¡getStockPrice(symbol); ¡ ¡ ¡ ¡ ¡return ¡price; ¡ } ¡ ¡ > ¡ spawn (getStockprice(“Pfizer”)). ¡ ¡ ¡ ¡ ¡ ¡ ¡ then ( ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡price ¡=> ¡console.log(price), ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡error ¡=> ¡console.error(error)); ¡ ¡ > ¡ ¡ 353.22 ¡ ¡ ¡

  17. Async Iteration Consumer Producer “PFE” ¡ result.then(data ¡=> ¡…, ¡error ¡=> ¡…) ¡ var ¡generator ¡= ¡getStockPrice(‘Pfizer’); ¡ var ¡result ¡= ¡generator.next() ¡ var ¡symbol ¡= ¡ yield ¡getStockSymbol(‘Pfizer’); ¡

  18. Async Iteration Consumer Producer 27.83 ¡ “PFE” ¡ var ¡result ¡= ¡generator. next ( “PFE” ) ¡ result.then(data ¡=> ¡…, ¡error ¡=> ¡…) ¡ var ¡symbol ¡= ¡ yield ¡getStockSymbol(‘Pfizer’); ¡ var ¡symbol ¡= ¡ “PFE” ¡ var ¡price ¡= ¡ yield ¡getStockPrice(symbol); ¡

  19. Async Iteration Consumer Producer 27.83 ¡ done ¡ var ¡result ¡= ¡generator. next ( “27.83” ) ¡ var ¡price ¡= ¡ yield ¡getStockPrice(symbol); ¡ var ¡price ¡= ¡27.83 ¡ return ¡price ¡

  20. Asynchronous Iteration ¡ function* ¡getStockPrice(name) ¡{ ¡ function* ¡getStockPrice(name) ¡{ ¡ producer ¡ ¡ ¡ ¡ ¡var ¡symbol ¡= ¡yield ¡getStockSymbol(name); ¡ ¡ ¡ ¡ ¡var ¡symbol ¡= ¡ symbolPromise; ¡ “JNJ”; ¡ ¡ ¡ ¡ ¡var ¡price ¡= ¡yield ¡getStockPrice(symbol) ¡ ¡ ¡ ¡ ¡ ¡var ¡price ¡= ¡ 420.25; ¡ pricePromise; ¡ ¡ ¡ ¡ ¡ ¡return ¡price; ¡ ¡ ¡ ¡ ¡return ¡price; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ } ¡ } ¡ ¡ ¡ ¡ function ¡spawn(generator) ¡{ ¡ function ¡spawn(generator) ¡{ ¡ function ¡spawn(generator) ¡{ ¡ ¡ ¡ ¡ ¡ return ¡new ¡Promise (( accept, ¡reject) ¡=> ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡new ¡Promise((accept, ¡reject) ¡=> ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡new ¡Promise((accept, ¡reject) ¡=> ¡{ ¡ ¡ ¡ ¡ ¡ ¡ consumer ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡var ¡onResult ¡= ¡lastPromiseResult ¡=> ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡var ¡ onResult ¡= ¡ “JNJ” ¡ 420.25 ¡ {value: ¡420.25, ¡done: ¡true}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡var ¡{value, ¡done} ¡= ¡generator.next(lastPromiseResult); ¡ “JNJ” ¡ {value: ¡symbolPromise, ¡done: ¡false}; ¡ {value: ¡pricePromise, ¡done: ¡false}; ¡ 420.25 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(!done) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(!done) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡value. then ( onResult , ¡reject); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡value.then(onResult, ¡reject); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡accept(value); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡accept(value); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡onResult(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡onResult(); ¡ ¡ ¡ ¡ ¡ ¡ }); ¡ ¡ ¡ ¡ ¡}); ¡ ¡ ¡ ¡ ¡}); ¡ } ¡ } ¡ } ¡ ¡ ¡ ¡ > ¡spawn(getStockPrice(“Johnson ¡and ¡Johnson”)).then(console.log); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡then (console.log); ¡ > ¡spawn(getStockPrice(“Johnson ¡and ¡Johnson”)).then(console.log);§ ¡ > ¡ 420.25 ¡

  21. Waiting with with Task.js function * ¡getStockPrice(name) ¡{ ¡ ¡ ¡ ¡ ¡var ¡symbol ¡= ¡ yield ¡getStockSymbol(name); ¡ ¡ ¡ ¡ ¡var ¡price ¡= ¡ yield ¡getStockPrice(symbol); ¡ ¡ ¡ ¡ ¡return ¡price; ¡ } ¡ ¡ var ¡result ¡= ¡ ¡ ¡ ¡ ¡ ¡spawn (getStockPrice.bind(null, ¡“Pfizer”)); ¡ ¡ result.then(console.log, ¡console.error); ¡

  22. Get Task.js

  23. Why should easy waiting require a library? ? ?

  24. Async Functions ES7 ES7

  25. Async Functions in ES7 function * ¡getStockPrice(name) ¡{ ¡ async ¡function ¡getStockPrice(name) ¡{ ¡ ¡ ¡ ¡ ¡var ¡symbol ¡= ¡yield ¡getStockSymbol(name); ¡ ¡ ¡ ¡ ¡var ¡symbol ¡= ¡ await ¡getStockSymbol(name); ¡ ¡ ¡ ¡ ¡var ¡price ¡= ¡yield ¡getStockPrice(symbol); ¡ ¡ ¡ ¡ ¡var ¡price ¡= ¡ await ¡getStockPrice(symbol); ¡ ¡ ¡ ¡ ¡return ¡price; ¡ ¡ ¡ ¡ ¡return ¡price; ¡ } ¡ } ¡ ¡ ¡ var ¡result ¡= ¡ ¡ ¡ ¡ ¡ ¡ ¡ getStockPrice ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡(“PFE”) ¡; ¡ var ¡result ¡= ¡ spawn (getStockPrice.bind(null, ¡“PFE”)); ¡ result.then(console.log, ¡console.error); ¡ result.then(console.log, ¡console.error); ¡

  26. function ¡getStockPrice(name) ¡{ ¡ ¡ ¡ ¡ ¡var ¡symbol ¡= ¡getStockSymbol(name); ¡ sync ¡ ¡ ¡ ¡var ¡price ¡= ¡getStockPrice(symbol); ¡ ¡ ¡ ¡ ¡return ¡price; ¡ } ¡ ¡ async ¡function ¡getStockPrice(name) ¡{ ¡ ¡ ¡ ¡ ¡var ¡symbol ¡= ¡ await ¡getStockSymbol(name); ¡ async ¡ ¡ ¡ ¡ ¡var ¡price ¡= ¡ await ¡getStockPrice(symbol); ¡ ¡ ¡ ¡ ¡return ¡price; ¡ } ¡

  27. Symmetrical support for push and pull functions. ES7 ES7

  28. Async Functions

  29. Try Async Functions

  30. The await keyword makes it easy to wait on an async value…

  31. …but what if you need to wait on an async stream of values? ? ?

  32. Waiting on a stream with for…on async ¡function ¡getNextPriceSpike(stock, ¡threshold) ¡{ ¡ ¡ ¡ ¡ ¡var ¡delta, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡oldPrice, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡price; ¡ ¡ ¡ ¡ ¡ ¡for(var ¡price ¡ on ¡new ¡WebSocket(“/prices/" ¡+ ¡stock)) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(oldPrice ¡== ¡null) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡oldPrice ¡= ¡price; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡delta ¡= ¡Math.abs(price ¡-­‑ ¡oldPrice); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡oldPrice ¡= ¡price; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(delta ¡> ¡threshold) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ return ¡ { ¡price: ¡price, ¡oldPrice: ¡oldPrice ¡}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡} ¡ } ¡ ¡ > ¡getNextPriceSpike(“JNJ”,2.50).then(diff ¡=> ¡console.log(diff)); ¡ > ¡{ ¡price: ¡420.22, ¡oldPrice: ¡423.19 ¡} ¡

  33. Unfortunately there’s a problem.

  34. The web has no standard Observable Observable interface.

  35. Iteration Observation Consumer Producer Consumer

  36. Observation Producer Consumer b ¡ b ¡ b ¡ b ¡ done ¡ 42 ¡ 39 ¡ observer. next (39) ¡ observer. next (42) ¡ observer. return () ¡ producer. observer (observer); ¡

  37. The Web’s Many Observable APIs • DOM Events • Websockets • Server-sent Events • Node Streams • Service Workers • jQuery Events • XMLHttpRequest • setInterval push

  38. Introducing Observable interface ¡Iterable ¡{ ¡ ¡Generator ¡iterator(void); ¡ } ¡ interface ¡Observable ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡observer(Generator); ¡ void ¡ } ¡

  39. Observer pattern > ¡nums(). observer ({ ¡ ¡ ¡ ¡ ¡ ¡ next (v) ¡{ ¡console.log(v); ¡}, ¡ ¡ ¡ ¡ ¡return (v) ¡{ ¡console.log(“done:” ¡+ ¡v); ¡} ¡ ¡ ¡ ¡ ¡ throw ¡ (e) ¡{ ¡console.error(e); ¡}, ¡ }); ¡ ¡ ¡ ¡ ¡ ¡ > ¡1 ¡ > ¡2 ¡ ¡ > ¡done: ¡3 ¡ ¡ ¡ ¡ ¡ ¡

  40. Adding Sugar to Observation nums(). observer ({ ¡ ¡ ¡ ¡ ¡ ¡next(v) ¡ ¡ ¡{ ¡ ¡console.log(v); ¡}, ¡ ¡ ¡ ¡ ¡ return(v) ¡{ ¡ ¡console.log(“done:” ¡+ ¡v); ¡} ¡ ¡ ¡ ¡ ¡throw ¡ (e) ¡{ ¡ ¡console.error(e); ¡}, ¡ }); ¡ nums(). ¡ ¡ ¡ ¡ ¡forEach (v ¡=> ¡console.log(v)). ¡ ¡ ¡ ¡ ¡ ¡// ¡next ¡ ¡ ¡ ¡ ¡ then ( ¡ ¡ ¡v ¡=> ¡console.log(“done:”), ¡// ¡return ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ e ¡=> ¡console.error(e)); ¡ ¡ ¡ ¡// ¡throw ¡ ¡ ( async ¡function ¡writeNums() ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡try ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ for (var ¡v ¡ on ¡nums()) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡console.log(v); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡} ¡catch(e) ¡{ ¡console.error(e); ¡} ¡ ¡ ¡ ¡ ¡console.log(“done”); ¡ })(); ¡ ¡ ¡ ¡ ¡ ¡

  41. Push APIs implement Observable DOM Events Websockets Server-sent Events Node Streams Observable Service Workers jQuery Events XMLHttpRequest setInterval

  42. Implementing Observable WebSocket .prototype.observer ¡= ¡function(generator) ¡{ ¡ ¡ ¡ ¡ ¡var ¡cleanUp, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡message ¡= ¡v ¡=> ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡var ¡pair ¡= ¡generator. next (v); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(pair. done ) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡cleanUp(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡error ¡= ¡e ¡=> ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡cleanUp(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡generator. throw (e); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡close ¡= ¡v ¡=> ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡cleanUp(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡generator. return (v); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}; ¡ ¡ ¡ ¡ ¡ ¡cleanUp ¡= ¡() ¡=> ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡this.removeEventListener(“message”, ¡message); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡this.removeEventListener(“error”, ¡error); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡this.removeEventListener(“close”, ¡close); ¡ ¡ ¡ ¡ ¡}; ¡ ¡ ¡ ¡ ¡this.addEventListener(“message”, ¡message); ¡ ¡ ¡ ¡ ¡this.addEventListener(“error”, ¡error); ¡ ¡ ¡ ¡ ¡this.addEventListener(“close”, ¡close); ¡ }; ¡

  43. Consuming Observables in ES7 > ¡( async ¡function() ¡{ ¡ ¡ for (var ¡member ¡ on ¡new ¡WebSocket(“/signups”)){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡console.log(JSON.stringify(member)); ¡ ¡ ¡ ¡ ¡} ¡ }()); ¡ > ¡{ ¡firstName: ¡“Tom” ¡… ¡} ¡ ¡ > ¡{ ¡firstName: ¡“John” ¡… ¡} ¡ ¡ ¡ > ¡{ ¡firstName: ¡“Micah” ¡… ¡} ¡ ¡ ¡ > ¡{ ¡firstName: ¡“Alex” ¡… ¡} ¡ ¡ ¡ > ¡{ ¡firstName: ¡“Yehuda” ¡… ¡} ¡ ¡ ¡ > ¡{ ¡firstName: ¡“Dave” ¡… ¡} ¡ ¡ ¡ > ¡{ ¡firstName: ¡“Ben” ¡… ¡} ¡ ¡ ¡ > ¡{ ¡firstName: ¡“Dave” ¡… ¡} ¡

  44. Observable Composition async ¡function ¡drag(element) ¡{ ¡ ¡ ¡ ¡var ¡downs ¡= ¡Observable.fromEvent(element, ¡“mousedown”); ¡ ¡ ¡ ¡var ¡ups ¡= ¡Observable.fromEvent(document, ¡“mouseup”); ¡ ¡ ¡ ¡var ¡moves ¡= ¡Observable.fromEvent(document, ¡“mousemoves”); ¡ ¡ ¡ ¡ ¡var ¡drags ¡= ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡downs. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡map (down ¡=> ¡moves. takeUntil (ups)). ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ flatten (); ¡ ¡ ¡ ¡for(var ¡drag ¡ on ¡drags) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡element.style.top ¡= ¡drag.offsetY; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡element.style.left ¡= ¡drag.offsetX; ¡ ¡ ¡} ¡ } ¡

  45. Try Observable

  46. If an async async function returns a Promise, and a function function* returns an Iterator…

  47. …what does an async async function function* return? ? ?

  48. Function Types in ES7 Synchr Synchronous onous Asynchr Asynchronous onous function T Promise function* * Iterator

  49. A function* returns multiple values. done ¡ 96 ¡ 23 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend