The document summarizes the evolution of asynchronous JavaScript programming. It begins with callbacks being the fundamental way to handle asynchronous code. This led to issues like callback hell and loss of error handling. Promises were introduced to help address these issues by providing a cleaner way to handle asynchronous operations with methods like then() and catch(). However, promises could still result in unreadable code if used improperly for control flow. Generators and async/await were later introduced to allow asynchronous code to be written in a synchronous, blocking-like fashion using keywords like yield and await. Overall, the document traces the progression from callbacks to promises to generators and async/await as ways to improve the handling of asynchronous code in JavaScript.
15. JS IS NON-BLOCKING
JS unlike a lot of other languages
never blocks
handling I/O is typically performed
via events and callbacks
16. JS unlike a lot of other languages
never blocks
handling I/O is typically performed
via events and callbacks
//cart
loadUserInfo()
loadCartItems()
retrieveExchangeRates()
calculateTotal()
17. JS unlike a lot of other languages
never blocks
handling I/O is typically performed
via events and callbacks
function getTotal() {
let total = 0
try {
const userInfo = loadUserInfo()
const cartItems = loadCartItems()
const exr = retrieveExchangeRates()
total = calculateTotal(userInfo, cartItems, exr)
} catch (error) {
}
return total
}
18. JS unlike a lot of other languages
never blocks
handling I/O is typically performed
via events and callbacks
function getTotal() {
let total = 0
try {
const userInfo = loadUserInfo()
const cartItems = loadCartItems()
const exr = retrieveExchangeRates()
total = calculateTotal(userInfo, cartItems, exr)
} catch (error) {
}
return total
}
can we do this?
19. JS unlike a lot of other languages
never blocks
handling I/O is typically performed
via events and callbacks
function getTotal() {
let total = 0
try {
const userInfo = loadUserInfo()
const cartItems = loadCartItems()
const exr = retrieveExchangeRates()
total = calculateTotal(userInfo, cartItems, exr)
} catch (error) {
}
return total
}
we could butâŠ..
we are blocking!
20. JS unlike a lot of other languages
never blocks
handling I/O is typically performed
via events and callbacks
function getTotal() {
let total = 0
try {
const userInfo = loadUserInfo()
const cartItems = loadCartItems()
const exr = retrieveExchangeRates()
total = calculateTotal(userInfo, cartItems, exr)
} catch (error) {
}
return total
}
we have to âwaitâ
50. I †CALLBACK
Callbacks are the fundamental unit
of asynchrony in JS
But sometimes theyâre not enough
for the evolving landscape
of async programming as JS matures
52. JS unlike a lot of other languages
never blocks
handling I/O is typically performed
via events and callbacks
function getTotal() {
let total = 0
try {
const userInfo = loadUserInfo()
const cartItems = loadCartItems()
const exr = retrieveExchangeRates()
total = calculateTotal(userInfo, cartItems, exr)
} catch (error) {
}
return total
}
54. PROMISE
This lets asynchronous methods return
values like synchronous methods
instead of the ïŹnal value
we get a promise.
A promise represents a proxy for a value
not necessarily known
when the promise is created
55.
56. Up until ES2015/ES6,
JS itself has actually
never had any direct notion of
asynchrony built into it
PROMISE
57. JS runs inside a hosting environment
(the browser/nodejs)
The event loop is handled by it
PROMISE
83. JS unlike a lot of other languages
never blocks
handling I/O is typically performed
via events and callbacks
function getTotal() {
let total = 0
try {
const userInfo = loadUserInfo()
const cartItems = loadCartItems()
const exr = retrieveExchangeRates()
total = calculateTotal(userInfo, cartItems, exr)
} catch (error) {
}
return total
}
87. GENERATOR
function *foo(){
let x = 1
yield
return x
}
const g = foo()
let n = g.next()
console.log(n)
// {value: undefined, done: false}
n = g.next()
console.log(n)
// {value: 1, done: true}
88. GENERATOR
function *foo(){
let x = 1
yield
return x
}
const g = foo()
let n = g.next()
console.log(n)
// {value: undefined, done: false}
n = g.next()
console.log(n)
// {value: 1, done: true}
89. GENERATOR
function *foo(){
let x = 1
yield
return x
}
const g = foo()
let n = g.next()
console.log(n)
// {value: undefined, done: false}
n = g.next()
console.log(n)
// {value: 1, done: true}
90. GENERATOR
function *foo(){
let x = 1
yield
return x
}
const g = foo()
let n = g.next()
console.log(n)
// {value: undefined, done: false}
n = g.next()
console.log(n)
// {value: 1, done: true}
98. GENERATOR
function *bar(){
let x = 14
let y = yield * x
return y
}
const g = bar()
let n = g.next()
console.log(n)
//{value: undefined, done: false}
n = g.next(3)
console.log(n)
//{value: 42, done: true}
99. function *bar(){
let x = 14
let y = yield * x
return y
}
GENERATOR
const g = bar()
let n = g.next()
console.log(n)
//{value: undefined, done: false}
n = g.next(3)
console.log(n)
//{value: 42, done: true}
100. function *bar(){
let x = 14
let y = yield * x
return y
}
GENERATOR
const g = bar()
let n = g.next()
console.log(n)
//{value: undefined, done: false}
n = g.next(3)
console.log(n)
//{value: 42, done: true}
101. function *bar(){
let x = 14
let y = yield * x
return y
}
GENERATOR
const g = bar()
let n = g.next()
console.log(n)
//{value: undefined, done: false}
n = g.next(3)
console.log(n)
//{value: 42, done: true}
106. GENERATOR + PROMISE
the iterator should listen for the promise to
resolve (or reject)
then either resume the generator with the
fulfilment message
or throw an error into the generator with
the rejection reason)
107. GENERATOR + PROMISE
function *getTotal() {
let total = 0
try {
const userInfo = yield loadUserInfo()
const cartItems = yield loadCartItems()
const exr = yield retrieveExchangeRates()
total = calculateTotal(userInfo, cartItems, exr)
} catch (error) {
}
return total
}
112. ASYNC/AWAIT
async function getTotal() {
let total = 0
try {
const userInfo = await loadUserInfo()
const cartItems = await loadCartItems()
const exr = await retrieveExchangeRates()
total = calculateTotal(userInfo, cartItems, exr)
} catch (error) {
}
return total
}
113. JS unlike a lot of other languages
never blocks
handling I/O is typically performed
via events and callbacks
function getTotal() {
let total = 0
try {
const userInfo = loadUserInfo()
const cartItems = loadCartItems()
const exr = retrieveExchangeRates()
total = calculateTotal(userInfo, cartItems, exr)
} catch (error) {
}
return total
}