Understand asynchronous JavaScript better than 99% of devs by building your own Promise implementation.
JavaScript core is synchronous.
When building frontend applications we often need to deal with asynchronous operations, such as user input or HTTP requests.
How do we deal with this asynchronicity in a synchronous language like JavaScript?
Callbacks
deal asynchronous code and lead to callback hell.
Promises
make callbacks manageable. understand how Promise API work.
Async/await
use the new API to make Promises even easier to work with.
Synchronicity reduces the cognitive load when writing Javascript code
A callback is a fucntion that’s passed as an argument to another function to be called by that function at a later time.
user events are asynchronous.
Problems
with callbacks
:
Promises
Promise states
:
pending
, set initiallyfulfilled
, successfullyrejected
, failureWork with promises
promise.then
promise.catch
promise.finally
As long as the promise is pending, the callbacks will not be executed.