Fireship · @fireship
JavaScript Promise in 100 Seconds
Uploaded January 31, 2020 · Length 1:39
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.
- 0:00
a promise represents a value that is
- 0:02
unknown now that may become known in the
- 0:04
future in other words in asynchronous
- 0:05
value think of it like a ride hailing
- 0:07
app when you request a ride the driver
- 0:09
makes a promise to pick you up while
- 0:12
you're waiting the ride is pending in
- 0:13
the future if all goes according to plan
- 0:15
the driver will resolve to pick you up
- 0:17
then take you somewhere
- 0:18
at which point your ride has been
- 0:20
fulfilled but in some cases the driver
- 0:22
might reject your ride in which case
- 0:24
you'll need to catch one somewhere else
- 0:25
either way the original request is now
- 0:27
finally settled as a developer he might
- 0:30
want to create a promise to represent an
- 0:32
asynchronous value but more often than
- 0:33
not you'll be consuming promises to use
- 0:36
the result of an asynchronous operation
- 0:37
in your code on the Left we're making
- 0:39
promises on the right we're using them
- 0:41
when constructed a promise starts off in
- 0:43
a pending state it's your job to define
- 0:46
a callback function called an executor
- 0:48
that defines when to resolve or reject
- 0:50
the promise this is where you would kick
- 0:51
off your asynchronous work on the other
- 0:53
side the consumer of the promise has
- 0:55
called it's then method it's waiting for
- 0:57
the asynchronous value to be fulfilled
- 0:59
when that happens it will call this
- 1:01
function with the value as its argument
- 1:03
we fulfill the promise by calling
- 1:05
resolve but there's always the
- 1:06
possibility of an exception in that case
- 1:08
we can reject the promise and send the
- 1:10
error back down to the consumer which
- 1:12
can use the catch method and an entirely
- 1:14
different function for handling
- 1:15
exceptions and if you just want to run
- 1:17
some code no matter what you can use
- 1:18
finally to handle both possibilities all
- 1:21
of these methods return promises which
- 1:23
means they can be chained together to
- 1:24
handle multiple asynchronous operations
- 1:26
in a row this has been JavaScript
- 1:27
promises in 100 seconds thanks for
- 1:30
watching and also make sure to follow my
- 1:31
brand new Instagram account for even
- 1:33
more content on this topic I will see
- 1:35
you in the next one
- 1:37
[Music]


