SlideShare a Scribd company logo
Introducing Async/Await
Valeri Karpov
@code_barbarian
github.com/vkarpov15
About Me
● Lead for Mongoose, Node+MongoDB ODM
● Author, Mastering Async/Await (ebook)
● Blogger, thecodebarbarian.com
● Invented the term “MEAN stack”
● 3rd async/await workshop: SF, Zagreb
Workshop Schedule
● 3:00-3:10 Intro, Return Values
● 3:10-3:40 Exercise 1
● 3:40-3:50 Error Handling
● 3:50-4:20 Exercise 2
● 4:20-4:25 Wrap-up and Takeaways
What Is Async/Await?
● 2 new keywords
● Async: special function that returns a promise
● Await: pauses execution of an async function
Callback Hell
● Error handling
● Readability
Async/Await Makes Async Logic Flat
Loops, If Statements, Try/Catch Work
Can Only Await Within An Async Function
TLDR; don’t use forEach() with async/await*
A Brief Overview of Promises
● Promise = state machine
● Represents async op
● Can fulfill with a value
● Or reject with an error
● await only handles promises
Await and Assignment
● Promise fulfilled value
Composing Async Functions
● Async functions return a promise
● Referred to as the returned promise
Resolved Value vs Return Value
The value you return from an async function is
not the return value! Await unwraps the promise
Exercise 1: Gather Blog Post Comments
● Suppose you have an API with 2 endpoints:
○ /post?id=${id}
○ /posts
Exercise 1: Gather Blog Post Comments
● fetch() a list of blog posts
● fetch() the content of each blog post
● Find the id of the first post whose content
contains “async/await hell”
● http://bit.ly/async-await-exercise-1
Part 2: Error Handling
● await on a fulfilled promise returns the value
● await on a rejected promise throws an error
Consolidated Error Handling
● 3 different patterns to handle all CB errors
Consolidated Error Handling
● Async function try/catch handles sync errors
Unhandled Errors Become Rejections
● Throwing rejects the returned promise
Rejected Value vs Sync Error
● Rejected value like resolved value for errors
Await Throws, Not the Function Call
Should You Use Try/Catch?
● catch() works too, often a better choice
Try/Catch vs. catch()
● Try/catch for specific, catch() for general
● Don’t use try/catch to wrap the entire function
Exercise 2: Retrying Failed Requests
● Exercise 1 assumed the API was reliable
● What about if every 2nd request fails?
● Need to wrap fetch() to retry 3 times
● http://bit.ly/async-await-exercise-2
Key Takeaways
● Async functions always return a promise
● return resolves the returned promise
● throw rejects the returned promise
● await pauses execution until promise settles
● await p returns the value p is fulfilled with
Further Reading
● http://bit.ly/node-promises-from-scratch
● http://bit.ly/async-await-design-patterns
● http://bit.ly/node-async-await
● The 80/20 Guide to ES2015 Generators
Thanks for Attending!
The Mastering Async/Await Ebook, June 14, 2018
asyncawait.net/wyncode

More Related Content

PDF
Intro to Asynchronous Javascript
PDF
JavaScript Promises
PDF
Asynchronous JavaScript Programming
PPTX
C# Async Await
PPTX
Solid principles
PDF
Asynchronous javascript
PDF
NodeJS for Beginner
PPTX
JavaScript Promises
Intro to Asynchronous Javascript
JavaScript Promises
Asynchronous JavaScript Programming
C# Async Await
Solid principles
Asynchronous javascript
NodeJS for Beginner
JavaScript Promises

What's hot (20)

PPTX
Async/Await
PDF
Asynchronous JavaScript Programming with Callbacks & Promises
PPTX
Introduction to Node.js
PPTX
JS Event Loop
PDF
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
PPTX
Core java complete ppt(note)
PDF
Spring Cloud Gateway
PPT
Spring ppt
PPTX
Methods in java
PPT
TypeScript Presentation
PPTX
Spring boot Introduction
PDF
Understanding Reactive Programming
PPTX
Angular modules in depth
PPT
Angular Introduction By Surekha Gadkari
PPS
Java Exception handling
PPTX
Introduction to Node js
PPT
Exception Handling in JAVA
PPTX
React hooks
PDF
Express node js
Async/Await
Asynchronous JavaScript Programming with Callbacks & Promises
Introduction to Node.js
JS Event Loop
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Core java complete ppt(note)
Spring Cloud Gateway
Spring ppt
Methods in java
TypeScript Presentation
Spring boot Introduction
Understanding Reactive Programming
Angular modules in depth
Angular Introduction By Surekha Gadkari
Java Exception handling
Introduction to Node js
Exception Handling in JAVA
React hooks
Express node js
Ad

Similar to Introducing Async/Await (20)

PDF
Mastering Async/Await in JavaScript
PDF
Async Await for Mobile Apps
PPTX
Async discussion 9_29_15
PDF
How Booking.com avoids and deals with replication lag
PPTX
Switch case and looping jam
PPTX
JS Fest 2018. Алексей Волков. Полезные инструменты для JS разработки
PPTX
Switch case and looping
PDF
London SF Developers: Custom Lightning Component Error Handling
PDF
Finding bugs in the code of LLVM project with the help of PVS-Studio
PPTX
Compose Camp - Session3.pptx
PPTX
Ecma script
PPTX
Task parallel library presentation
PDF
Avoiding callback hell with promises
PDF
Play Framework
PPTX
Macasu, gerrell c.
PPT
Asynchronous in dot net4
PDF
Open World Forum 2014 : From ES6 to Javascript 2.0. What use today ? par Jon...
PDF
Getting Comfortable with JS Promises
PPTX
Asynchronous programming - .NET Way
PPTX
My final requirement
Mastering Async/Await in JavaScript
Async Await for Mobile Apps
Async discussion 9_29_15
How Booking.com avoids and deals with replication lag
Switch case and looping jam
JS Fest 2018. Алексей Волков. Полезные инструменты для JS разработки
Switch case and looping
London SF Developers: Custom Lightning Component Error Handling
Finding bugs in the code of LLVM project with the help of PVS-Studio
Compose Camp - Session3.pptx
Ecma script
Task parallel library presentation
Avoiding callback hell with promises
Play Framework
Macasu, gerrell c.
Asynchronous in dot net4
Open World Forum 2014 : From ES6 to Javascript 2.0. What use today ? par Jon...
Getting Comfortable with JS Promises
Asynchronous programming - .NET Way
My final requirement
Ad

More from Valeri Karpov (20)

PDF
A Practical Introduction to GeoJSON
PDF
A Practical Introduction to Functions-as-a-Service
PDF
A Gentle Introduction to Functions-as-a-Service
PDF
TAO and the Essence of Modern JavaScript
PDF
React, Redux, and Archetype
PDF
TDD a REST API With Node.js and MongoDB
PDF
Conquering AngularJS Limitations
PDF
MongoDB MEAN Stack Webinar October 7, 2015
PDF
MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQ...
PDF
Lessons in Open Source from the MongooseJS ODM
PDF
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)
PDF
MongoDB Miami Meetup 1/26/15: Introduction to WiredTiger
PDF
MongoDB API Talk @ HackPrinceton
PDF
MEAN Stack WeNode Barcelona Workshop
PDF
MongoDB Israel June Meetup
PDF
JS-IL: Getting MEAN in 1 Hour
PDF
JS-IL Keynote: MongoDB 2.6, Mongoose 4.0, and Beyond
PDF
MEAN Stack Workshop at Node Philly, 4/9/14
PDF
MongoDB: Queries and Aggregation Framework with NBA Game Data
PDF
Mongo db in 3 minutes BoilerMake
A Practical Introduction to GeoJSON
A Practical Introduction to Functions-as-a-Service
A Gentle Introduction to Functions-as-a-Service
TAO and the Essence of Modern JavaScript
React, Redux, and Archetype
TDD a REST API With Node.js and MongoDB
Conquering AngularJS Limitations
MongoDB MEAN Stack Webinar October 7, 2015
MEAN Stack NYC Meetup 20150717: TDD Your AngularJS + Ionic Directives With jQ...
Lessons in Open Source from the MongooseJS ODM
Nimrod: MongoDB Shell in NodeJS (JSConfUY 2015)
MongoDB Miami Meetup 1/26/15: Introduction to WiredTiger
MongoDB API Talk @ HackPrinceton
MEAN Stack WeNode Barcelona Workshop
MongoDB Israel June Meetup
JS-IL: Getting MEAN in 1 Hour
JS-IL Keynote: MongoDB 2.6, Mongoose 4.0, and Beyond
MEAN Stack Workshop at Node Philly, 4/9/14
MongoDB: Queries and Aggregation Framework with NBA Game Data
Mongo db in 3 minutes BoilerMake

Recently uploaded (20)

PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Approach and Philosophy of On baking technology
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Machine Learning_overview_presentation.pptx
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
1. Introduction to Computer Programming.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Machine learning based COVID-19 study performance prediction
SOPHOS-XG Firewall Administrator PPT.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Approach and Philosophy of On baking technology
Advanced methodologies resolving dimensionality complications for autism neur...
Machine Learning_overview_presentation.pptx
NewMind AI Weekly Chronicles - August'25-Week II
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
1. Introduction to Computer Programming.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
The Rise and Fall of 3GPP – Time for a Sabbatical?
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Encapsulation_ Review paper, used for researhc scholars
Unlocking AI with Model Context Protocol (MCP)
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Digital-Transformation-Roadmap-for-Companies.pptx

Introducing Async/Await