Fireship · @fireship

JavaScript Promise in 100 Seconds

Uploaded January 31, 2020 · Length 1:39

JavaScript Promise in 100 Seconds

Summary

javascript promises explained in 100 seconds with examples and use cases.

  • javascript promises
  • async programming
  • promise examples
  • javascript tutorial

Transcript preview

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

  1. 0:00

    a promise represents a value that is

  2. 0:02

    unknown now that may become known in the

  3. 0:04

    future in other words in asynchronous

  4. 0:05

    value think of it like a ride hailing

  5. 0:07

    app when you request a ride the driver

  6. 0:09

    makes a promise to pick you up while

  7. 0:12

    you're waiting the ride is pending in

  8. 0:13

    the future if all goes according to plan

  9. 0:15

    the driver will resolve to pick you up

  10. 0:17

    then take you somewhere

  11. 0:18

    at which point your ride has been

  12. 0:20

    fulfilled but in some cases the driver

  13. 0:22

    might reject your ride in which case

  14. 0:24

    you'll need to catch one somewhere else

  15. 0:25

    either way the original request is now

  16. 0:27

    finally settled as a developer he might

  17. 0:30

    want to create a promise to represent an

  18. 0:32

    asynchronous value but more often than

  19. 0:33

    not you'll be consuming promises to use

  20. 0:36

    the result of an asynchronous operation

  21. 0:37

    in your code on the Left we're making

  22. 0:39

    promises on the right we're using them

  23. 0:41

    when constructed a promise starts off in

  24. 0:43

    a pending state it's your job to define

  25. 0:46

    a callback function called an executor

  26. 0:48

    that defines when to resolve or reject

  27. 0:50

    the promise this is where you would kick

  28. 0:51

    off your asynchronous work on the other

  29. 0:53

    side the consumer of the promise has

  30. 0:55

    called it's then method it's waiting for

  31. 0:57

    the asynchronous value to be fulfilled

  32. 0:59

    when that happens it will call this

  33. 1:01

    function with the value as its argument

  34. 1:03

    we fulfill the promise by calling

  35. 1:05

    resolve but there's always the

  36. 1:06

    possibility of an exception in that case

  37. 1:08

    we can reject the promise and send the

  38. 1:10

    error back down to the consumer which

  39. 1:12

    can use the catch method and an entirely

  40. 1:14

    different function for handling

  41. 1:15

    exceptions and if you just want to run

  42. 1:17

    some code no matter what you can use

  43. 1:18

    finally to handle both possibilities all

  44. 1:21

    of these methods return promises which

  45. 1:23

    means they can be chained together to

  46. 1:24

    handle multiple asynchronous operations

  47. 1:26

    in a row this has been JavaScript

  48. 1:27

    promises in 100 seconds thanks for

  49. 1:30

    watching and also make sure to follow my

  50. 1:31

    brand new Instagram account for even

  51. 1:33

    more content on this topic I will see

  52. 1:35

    you in the next one

  53. 1:37

    [Music]