Functions prefixed with the
async keyword implicitly return a promise. When the function returns a value the promise is fulfilled.
The async implementation of
foo above is equivalent to the below implementation that explicitly returns a
await keyword to stop a function from continuing until an
async function completes.
Keep in mind that
await can only be used inside an
async function — hence why we use
.then().catch() when calling
bar() but inside
bar() we can
await the result of
Think of a
Promise like a container for a value that can only be known at some point in the future (like the result of a network call or reading a file from disk). They exist in one of three states:
Promise has callback functions that are executed when its state changes to handle success or failure. These callbacks can themselves be a
Promise allowing for
Promise’s to be chained.
resolve changes the promises state from pending to fulfilled causing the callback function in