167

Connect.Tech 2017: Stop JavaScripting Like It's 1999

 6 years ago
source link: https://www.slideshare.net/HunterLoftis1/connecttech-2017-stop-javascripting-like-its-1999
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Connect.Tech 2017: Stop JavaScripting Like It's 1999

  1. 1. @hunterloftis Stop JavaScripting Like It’s 1999 Connect.Tech 2017
  2. 2. @hunterloftis
  3. 3. @hunterloftis
  4. 4. @hunterloftis
  5. 5. @hunterloftis
  6. 6. @hunterloftis read package.json parse JSON fetch repository.url parse the body match “X users starred” in the body return the match 1 2 3 4 5 6 Fetching Stars
  7. 7. @hunterloftis 1 2 3 4 5 6
  8. 8. @hunterloftis Why would anyone use JavaScript?
  9. 9. @hunterloftis !== blocking/slow 🐢concurrent/fast 🚗
  10. 10. @hunterloftis concurrency tax
  11. 11. @hunterloftis catch errors passed as arguments catch errors from rejected promises catch thrown errors blocking/slow 🐢concurrent/fast 🚗 create new execution contexts
  12. 12. @hunterloftis That’s just how JavaScript works. worked
  13. 13. @hunterloftis new tools for tax-free concurrency node >= 7.6.0 (Feb 22, 2017)
  14. 14. @hunterloftis new tools for tax-free concurrency
  15. 15. @hunterloftis So? There’s always something new in JavaScript.
  16. 16. @hunterloftis 1. What does modern JS look like? 2. How can you start writing it? 3. Which obstacles should you expect?
  17. 17. @hunterloftis 1. What does modern JS look like?
  18. 18. @hunterloftis modern JS should read like Python concurrent/fast 🚗 …and run like JavaScript!
  19. 19. @hunterloftis How?
  20. 20. @hunterloftis Three Rules:
  21. 21. @hunterloftis 1. Whenever you think about writing a callback, use async / await instead.
  22. 22. @hunterloftis this is a callback function
  23. 23. @hunterloftis 2. throw and catch Errors, or allow them to bubble. But otherwise leave them alone!
  24. 24. @hunterloftis
  25. 25. @hunterloftis “Pyramid of Doom” “Callback Hell”
  26. 26. @hunterloftis 3.Instead of chaining Promises, use await + simple assignment.
  27. 27. @hunterloftis
  28. 28. @hunterloftis Is this still JavaScript? • no more callbacks • no more `if (err)` • no more Promises
  29. 29. @hunterloftis The three objections
  30. 30. @hunterloftis 1. I don’t understand async/await (which means I don’t understand Promises)
  31. 31. @hunterloftis
  32. 32. @hunterloftis “I return a Promise.” “I wait here for another Promise to resolve.” ?
  33. 33. @hunterloftis
  34. 34. @hunterloftis 2. Lots of modules still use callbacks (like node’s core APIs)
  35. 35. @hunterloftisJune 2017: node-core Promises!
  36. 36. @hunterloftisfor node < 8.0.0
  37. 37. @hunterloftis 3. I work on a huge codebase (and rewrites are terrible)
  38. 38. @hunterloftis
  39. 39. @hunterloftis What does a modern foo look like?
  40. 40. @hunterloftis async json = await… res = await… body = await… 1. no callbacks! 2. throw errors! 3. no chaining! what’s left?
  41. 41. @hunterloftisPython
  42. 42. @hunterloftis concurrent/fast 🚗 JavaScript
  43. 43. @hunterloftis Before After
  44. 44. @hunterloftis Questions? [email protected]

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK