Fireship · @fireship

The Async Await Episode I Promised

Uploaded October 11, 2018 · Length 12:04

The Async Await Episode I Promised

Summary

javascript async await explained with examples and code demos

  • javascript async await
  • event loop
  • promises
  • callbacks
  • async programming

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    [Music]

  2. 0:06

    javascript is a single-threaded

  3. 0:08

    programming language yet everything we

  4. 0:10

    do on the web tends to be blocking or

  5. 0:11

    time-consuming which means that

  6. 0:13

    asynchronous programming is an essential

  7. 0:15

    skill for any JavaScript developer

  8. 0:17

    today's video will focus primarily on

  9. 0:19

    the amazing syntactic sugar provided by

  10. 0:21

    async await but before we can get there

  11. 0:23

    we really need to understand things from

  12. 0:24

    the ground up starting with the browser

  13. 0:26

    or nodejs event loop callbacks promises

  14. 0:29

    and then finally async await and we're

  15. 0:31

    going to do this without making a single

  16. 0:33

    joke about a sink or a wave if you're

  17. 0:35

    new here like and subscribe and we'll be

  18. 0:37

    giving away another one of a kind

  19. 0:38

    t-shirt with this video all you have to

  20. 0:39

    do is leave a comment below and we'll

  21. 0:41

    pick a random winner next week in order

  22. 0:43

    to understand anything async you really

  23. 0:45

    need to first understand the event loop

  24. 0:47

    this is ace I hope that clears up any

  25. 0:50

    questions you have I'm going to give you

  26. 0:52

    the general overview but I highly

  27. 0:54

    recommend you watch this talk from Jake

  28. 0:55

    Archibald which is the best explanation

  29. 0:57

    of the event loop that I've ever seen

  30. 0:59

    then after you're done watching that

  31. 1:00

    talk you should check out Stephen

  32. 1:02

    fluence demos with angular Channel which

  33. 1:04

    just released a video about callbacks

  34. 1:05

    which are one of the things that happen

  35. 1:07

    within the event loop so what is the

  36. 1:09

    event loop and what does it have to do

  37. 1:10

    with async away both the browser and

  38. 1:13

    nodejs are always running a single

  39. 1:15

    threaded event loop to run your code on

  40. 1:17

    the first go around it will run all of

  41. 1:19

    your synchronous code but it might also

  42. 1:21

    queue up asynchronous events to be

  43. 1:22

    called back later you say here's a

  44. 1:24

    function that I need to run but first I

  45. 1:25

    need to go get some data from the

  46. 1:27

    network the event loop says okay I'll

  47. 1:29

    keep doing my thing while you do your

  48. 1:30

    thing in a separate thread pool then at

  49. 1:32

    some point in the future get data will

  50. 1:34

    finish and let the event loop know that

  51. 1:36

    it's ready to be called back now this is

  52. 1:38

    where things get interesting if it's a

  53. 1:39

    macro task like a set timeout or set

  54. 1:42

    interval it will be executed on the next

  55. 1:44

    event loop but if it's a micro task like

  56. 1:46

    a fulfilled promise then it will be

  57. 1:48

    called back before the start of the next

  58. 1:50

    event loop let's look at the

  59. 1:51

    implications of this by writing some

  60. 1:53

    actual code first we will write a

  61. 1:55

    console log which is synchronous then

  62. 1:57

    we'll throw in a set timeout but give it

  63. 1:58

    a time delay of zero milliseconds then