Intro to Asynchronous
Javascript
Garrett Welson
About me
• Graduate from Hack Reactor,
a software engineering
immersive program located at
Galvanize in downtown ATX
• Work at Hack Reactor as a
Resident, where I mentor
junior engineers, teach
concepts + best practices
• My first tech conference!
Goals
• Understand what asynchronous code is and why it
works differently from synchronous code
• The event loop
• Understand how to work with asynchronous code
• Callbacks 🙁 —> Promises 🙂 —> Async/Await 🤯
• Understand what’s happening under the hood with
these newer methods, and how to refactor old code
What is the event
loop?
Javascript 101
• Javascript is single-threaded
• Code is generally synchronous
• Javascript interprets each line right away
before moving on to what’s next
• Code is expected to be non-blocking
An early mistake…
let data = fetch('https://jsonplaceholder.typicode.com/posts')
console.log(data)
An early mistake…
let data = fetch('https://jsonplaceholder.typicode.com/posts')
console.log(data) // logs: Promise {<pending>}
Call Stack
const myName = "Garrett";
function sayHello(name) {
console.log(`Hello, ${name}
`);
}
sayHello(myName);
sayHello()
Call Stack
const myName = "Garrett";
function sayHello(name) {
console.log(`Hello, ${name}
`);
}
sayHello(myName);
sayHello()
console.log(“Garrett”)
Call Stack
const myName = "Garrett";
function sayHello(name) {
console.log(`Hello, ${name}
`);
}
sayHello(myName);
sayHello()
Call Stack
const myName = "Garrett";
function sayHello(name) {
console.log(`Hello, ${name}
`);
}
sayHello(myName);
What about
asynchronous code?
Some examples…
• Code that runs on a delay (setTimeout,
setInterval, etc.) 🕐
• Code that requests data from a webpage/API
(Ajax, Fetch, etc.) 🌐
• Code that requests data from a database or
filesystem (typically run server-side) 🗂
Some callback-based code
function sayHello(name) {
console.log(`Hello, ${name}`);
}
setTimeout(() => {
sayHello("Garrett")
}, 2000)
Call Stack Browser
APIs
Callback Queue
setTimeout()
Call Stack Browser
APIs
Callback Queue
setTimeout()
Call Stack Browser
APIs
Callback Queue
() => {}
Call Stack Browser
APIs
Callback Queue
() => {}
Call Stack Browser
APIs
Callback Queue
() => {}
sayHello()
Call Stack Browser
APIs
Callback Queue
() => {}
sayHello()
console.log()
Call Stack Browser
APIs
Callback Queue
() => {}
sayHello()
Call Stack Browser
APIs
Callback Queue
() => {}
Call Stack Browser
APIs
Callback Queue
A few choices
• Callbacks
• Promises
• Async/Await
Callbacks
Callbacks
• Just functions!
• Not any kind of special part of Javascript
• Function passed into another function to be run
on the result
Synchronous Example
function sayHello(person, response) {
console.log(`Hello, ${person}!`)
response()
}
function sayHiBack() {
console.log('Hi! Good to see you!')
}
sayHello('DevWeek', sayHiBack)
Synchronous Example
function sayHello(person, response) {
console.log(`Hello, ${person}!`)
response()
}
function sayHiBack() {
console.log('Hi! Good to see you!')
}
sayHello('DevWeek', sayHiBack)
// 'Hello, DevWeek!'
// 'Hi! Good to see you!
Some common examples
• Javascript APIs like setTimeout, setInterval, etc.
• jQuery AJAX requests
• Node-style callbacks
• Often in the form of (err, data) => { … }
The AJAX Way
$.ajax({
url: 'https://jsonplaceholder.typicode.com/users',
success: function(data) {
console.log(data);
},
error: function(request, error) {
console.log(error);
},
dataType: ‘json’
});
The AJAX Way
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},…
]
Advantages
• Straightforward (at first)
• Many examples
• Comfortable
Disadvantages
• Debugging
• Readability
• Complexity
Callback Hell
$.ajax({
url: 'https://jsonplaceholder.typicode.com/users',
success: function(data) {
const target = data[4];
const id = target.id;
$.ajax({
url: `https://jsonplaceholder.typicode.com/posts?userId=${id}`,
success: function(data) {
console.log(data)
},
error: function(request, error) {
console.log(error)
},
dataType: 'json'
});
},
error: function(request, error) {
console.log(error)
},
dataType: 'json'
});
Promises
What is a promise?
• “An object that represents the eventual
completion or failure of an asynchronous
operation”
• A much easier way to work with async code
Let’s look inside
The Promise Constructor
const newPromise = new Promise((resolve, reject) => {
// asynchronous operation here
if (error) {
reject(error); // rejected
}
resolve(someValue); // fulfilled
});
Why are they useful?
• Quicker to write
• Easily “chainable”
• Better at dealing with complex situations
Callback Hell
$.ajax({
url: 'https://jsonplaceholder.typicode.com/users',
success: function(data) {
const target = data[4];
const id = target.id;
$.ajax({
url: `https://jsonplaceholder.typicode.com/posts?userId=${id}`,
success: function(data) {
console.log(data)
},
error: function(request, error) {
console.log(error)
},
dataType: 'json'
});
},
error: function(request, error) {
console.log(error)
},
dataType: 'json'
});
With Promises…
fetch("https://jsonplaceholder.typicode.com/users")
.then(result => result.json())
.then(data => {
let id = data[4].id;
return fetch(`https://jsonplaceholder.typicode.com/
posts?userId=${id}`);
})
.then(result => result.json())
.then(posts => console.log(posts))
Building blocks
• .then()
• .catch()
• .finally()
Making a promise
• Native ES6 constructor
• Node’s util.promisify()
• Third-party libraries like Bluebird
Example
const wait = function(ms, value) {
return new Promise (function(resolve) {
setTimeout(() => resolve(value), ms)
})
}
wait(2000, "hello").then(result =>
{console.log(result)})
What about the event
loop?
Not quite the same
• Micro task queue
• .then()/.catch() statements take priority over
macro-tasks (like setTimeout or browser events)
that move into the normal event queue
Example
console.log('start');
setTimeout(function() {
console.log('end of timeout')
},0);
Promise.resolve("result of promise")
.then(value => console.log(value));
Example
console.log('start');
setTimeout(function() {
console.log('end of timeout')
},0);
Promise.resolve("result of promise")
.then(value => console.log(value));
// "start"
// "result of promise"
// "end of timeout"
Why is this useful?
• Granular control over the order of async actions
• Control over the state of the DOM/events
A few additional methods
• Promise.all( … )
• Array of results once all promises have resolved
• Promise.race( … )
• Result of first promise to resolve (or reason if
promise rejects)
• Promise.allSettled( … )
• Array of statuses of all promises (not results)
Promise.all()
fetch("https://jsonplaceholder.typicode.com/users")
.then(result => result.json())
.then(data => {
let id = data[4].id;
return fetch(`https://jsonplaceholder.typicode.com/posts?userId=${id}`);
})
.then(result => result.json())
.then(posts => posts.map(post => fetch(`https://jsonplaceholder.typicode.com/
comments?postId=${post.id}`)))
.then(promiseArr => Promise.all(promiseArr))
.then(responses => Promise.all(responses.map(response => response.json())))
.then(results => console.log(results))
Async/Await
Background
• Introduced in ES8
• Similar to generators
• Allows async code to be written more like
synchronous code
• Syntactic sugar on top of promises
Example
async function sayHello() {
return "Hello!"
}
sayHello().then(response => console.log(response))
Example
async function sayHello() {
return "Hello!"
}
sayHello().then(response => console.log(response))
// logs "Hello!"
Async Functions
• Must be declared with the async keyword
• Always return a promise
• Pause their execution when they hit the await
keyword
Refactoring
async function getComments() {
let users = await fetch("https://jsonplaceholder.typicode.com/users");
users = await users.json();
let id = users[4].id;
let posts = await fetch(`https://jsonplaceholder.typicode.com/posts?userId=${id}`);
posts = await posts.json();
let promiseArr = posts.map(post => await fetch( `https://jsonplaceholder.typicode.com/
comments?postId=${post.id}`));
let comments = await Promise.all(promiseArr)
comments = await Promise.all(comments.map(comment => comment.json()))
console.log(comments)
}
getComments()
Even cleaner
async function getComments() {
let users = await (await fetch("https://jsonplaceholder.typicode.com/users")).json();
let id = users[4].id;
let posts = await (await fetch(`https://jsonplaceholder.typicode.com/posts?userId=$
{id}`)).json()
let comments = await Promise.all(
posts.map(async (post) => await (await fetch( `https://jsonplaceholder.typicode.com/
comments?postId=${post.id}`)).json())
)
console.log(comments)
}
getComments()
Pros/Cons
• Lets us write code that looks and feels more
synchronous
• Allows us to easily use try/catch blocks inside of
async functions
• Sequential nature of code can make successive
async actions take longer
• Can use Promise.all() to avoid this
Wrapping Up
Takeaways
• Understand the event loop and how
asynchronous code is executed in Javascript
• Understand callbacks and their pros & cons
• Understand the pros & cons of promises and
async/await
• Understand how to refactor old code to utilize
newer methods
Thanks!

More Related Content

PDF
Asynchronous JavaScript Programming with Callbacks & Promises
PDF
Asynchronous JavaScript Programming
PPTX
Introduction to Node.js
PPTX
Flexbox
PDF
JavaScript - Chapter 6 - Basic Functions
PPT
Js ppt
PPTX
Basic Concept of Node.js & NPM
PPTX
Life skills 1--_communication
Asynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming
Introduction to Node.js
Flexbox
JavaScript - Chapter 6 - Basic Functions
Js ppt
Basic Concept of Node.js & NPM
Life skills 1--_communication

What's hot (20)

PPTX
JavaScript Promises
PDF
Angular - Chapter 4 - Data and Event Handling
PPT
Javascript
PDF
JavaScript - Chapter 7 - Advanced Functions
PPTX
Node.js File system & Streams
PPT
JavaScript & Dom Manipulation
PPT
JavaScript: Events Handling
PDF
Javascript
PDF
Introduction to Redux
PPT
Introduction to Javascript
PDF
Angular Directives
PPTX
PPTX
React/Redux
PPTX
Intro to React
PDF
Introducing Async/Await
PPTX
Express JS
PDF
Javascript essentials
PPT
Javascript
PPTX
JavaScript Basic
JavaScript Promises
Angular - Chapter 4 - Data and Event Handling
Javascript
JavaScript - Chapter 7 - Advanced Functions
Node.js File system & Streams
JavaScript & Dom Manipulation
JavaScript: Events Handling
Javascript
Introduction to Redux
Introduction to Javascript
Angular Directives
React/Redux
Intro to React
Introducing Async/Await
Express JS
Javascript essentials
Javascript
JavaScript Basic
Ad

Similar to Intro to Asynchronous Javascript (20)

PPTX
asyncjavascript.pptxdgdsgdffgfdgfgfgfdgfdgf
PPTX
How to perform debounce in react
PPTX
JavaScript Multithread or Single Thread.pptx
PPTX
Avoiding Callback Hell with Async.js
PDF
Asynchronous web apps with the Play Framework 2.0
KEY
Node.js
PPTX
Async Redux Actions With RxJS - React Rally 2016
PDF
How to make Ajax work for you
PPTX
Introduction to Vert.x
KEY
Writing robust Node.js applications
PPTX
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
PDF
An opinionated intro to Node.js - devrupt hospitality hackathon
PPT
JS everywhere 2011
KEY
Playing With Fire - An Introduction to Node.js
PDF
Asynchronous development in JavaScript
PPTX
Asynchronous Web Programming with HTML5 WebSockets and Java
PDF
Serverless Ballerina
PPT
Expert JavaScript tricks of the masters
PPTX
Event-driven IO server-side JavaScript environment based on V8 Engine
PDF
Intro to jquery
asyncjavascript.pptxdgdsgdffgfdgfgfgfdgfdgf
How to perform debounce in react
JavaScript Multithread or Single Thread.pptx
Avoiding Callback Hell with Async.js
Asynchronous web apps with the Play Framework 2.0
Node.js
Async Redux Actions With RxJS - React Rally 2016
How to make Ajax work for you
Introduction to Vert.x
Writing robust Node.js applications
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
An opinionated intro to Node.js - devrupt hospitality hackathon
JS everywhere 2011
Playing With Fire - An Introduction to Node.js
Asynchronous development in JavaScript
Asynchronous Web Programming with HTML5 WebSockets and Java
Serverless Ballerina
Expert JavaScript tricks of the masters
Event-driven IO server-side JavaScript environment based on V8 Engine
Intro to jquery
Ad

Recently uploaded (20)

PDF
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
PDF
The AI Revolution in Customer Service - 2025
PPTX
How to Convert Tickets Into Sales Opportunity in Odoo 18
PDF
The-2025-Engineering-Revolution-AI-Quality-and-DevOps-Convergence.pdf
PDF
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
PDF
CCUS-as-the-Missing-Link-to-Net-Zero_AksCurious.pdf
PDF
Lung cancer patients survival prediction using outlier detection and optimize...
PDF
Rapid Prototyping: A lecture on prototyping techniques for interface design
PDF
Decision Optimization - From Theory to Practice
PDF
Streamline Vulnerability Management From Minimal Images to SBOMs
PPTX
Information-Technology-in-Human-Society.pptx
PDF
SaaS reusability assessment using machine learning techniques
PPTX
Report in SIP_Distance_Learning_Technology_Impact.pptx
PDF
Launch a Bumble-Style App with AI Features in 2025.pdf
PDF
Examining Bias in AI Generated News Content.pdf
PPTX
Presentation - Principles of Instructional Design.pptx
PDF
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
PDF
Build Real-Time ML Apps with Python, Feast & NoSQL
PDF
A symptom-driven medical diagnosis support model based on machine learning te...
PDF
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
The AI Revolution in Customer Service - 2025
How to Convert Tickets Into Sales Opportunity in Odoo 18
The-2025-Engineering-Revolution-AI-Quality-and-DevOps-Convergence.pdf
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
CCUS-as-the-Missing-Link-to-Net-Zero_AksCurious.pdf
Lung cancer patients survival prediction using outlier detection and optimize...
Rapid Prototyping: A lecture on prototyping techniques for interface design
Decision Optimization - From Theory to Practice
Streamline Vulnerability Management From Minimal Images to SBOMs
Information-Technology-in-Human-Society.pptx
SaaS reusability assessment using machine learning techniques
Report in SIP_Distance_Learning_Technology_Impact.pptx
Launch a Bumble-Style App with AI Features in 2025.pdf
Examining Bias in AI Generated News Content.pdf
Presentation - Principles of Instructional Design.pptx
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
Build Real-Time ML Apps with Python, Feast & NoSQL
A symptom-driven medical diagnosis support model based on machine learning te...
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj

Intro to Asynchronous Javascript