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

What's hot (20)

PDF
javascript objects
Vijay Kalyan
 
PDF
Asynchronous javascript
Eman Mohamed
 
PPTX
Javascript functions
Alaref Abushaala
 
PPTX
Angular tutorial
Rohit Gupta
 
PPT
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
PPT
Introduction to Javascript
Amit Tyagi
 
PPTX
Event In JavaScript
ShahDhruv21
 
PPT
Java Script ppt
Priya Goyal
 
PPTX
Typescript ppt
akhilsreyas
 
PDF
ReactJS presentation
Thanh Tuong
 
PPTX
Rest & RESTful WebServices
Prateek Tandon
 
PPTX
Angular Data Binding
Jennifer Estrada
 
PPT
Advanced Javascript
Adieu
 
PDF
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
Edureka!
 
PDF
Angular Observables & RxJS Introduction
Rahat Khanna a.k.a mAppMechanic
 
PDF
ES6 presentation
ritika1
 
PDF
ES2015 / ES6: Basics of modern Javascript
Wojciech Dzikowski
 
PDF
Asynchronous JavaScript Programming
Haim Michael
 
PDF
Expressjs
Yauheni Nikanovich
 
PDF
Introduction to Redux
Ignacio MartĂ­n
 
javascript objects
Vijay Kalyan
 
Asynchronous javascript
Eman Mohamed
 
Javascript functions
Alaref Abushaala
 
Angular tutorial
Rohit Gupta
 
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
Introduction to Javascript
Amit Tyagi
 
Event In JavaScript
ShahDhruv21
 
Java Script ppt
Priya Goyal
 
Typescript ppt
akhilsreyas
 
ReactJS presentation
Thanh Tuong
 
Rest & RESTful WebServices
Prateek Tandon
 
Angular Data Binding
Jennifer Estrada
 
Advanced Javascript
Adieu
 
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
Edureka!
 
Angular Observables & RxJS Introduction
Rahat Khanna a.k.a mAppMechanic
 
ES6 presentation
ritika1
 
ES2015 / ES6: Basics of modern Javascript
Wojciech Dzikowski
 
Asynchronous JavaScript Programming
Haim Michael
 
Expressjs
Yauheni Nikanovich
 
Introduction to Redux
Ignacio MartĂ­n
 

Similar to Introducing Async/Await (20)

PDF
Mastering Async/Await in JavaScript
Valeri Karpov
 
PDF
Async Await for Mobile Apps
Craig Dunn
 
PPTX
Async discussion 9_29_15
Cheryl Yaeger
 
PDF
How Booking.com avoids and deals with replication lag
Jean-François GagnÊ
 
PPTX
Switch case and looping jam
JamaicaAubreyUnite
 
PPTX
JS Fest 2018. Алексей Волков. Полезные инструменты для JS разработки
JSFestUA
 
PPTX
Switch case and looping
aprilyyy
 
PDF
London SF Developers: Custom Lightning Component Error Handling
Richard Clark
 
PDF
Finding bugs in the code of LLVM project with the help of PVS-Studio
PVS-Studio
 
PPTX
Compose Camp - Session3.pptx
GDSCAtharvaCollegeOf
 
PPTX
Ecma script
MOHIT KUMAR
 
PPTX
Task parallel library presentation
ahmed sayed
 
PDF
Avoiding callback hell with promises
TorontoNodeJS
 
PDF
Play Framework
Eduard Tudenhoefner
 
PPTX
Macasu, gerrell c.
gerrell
 
PPT
Asynchronous in dot net4
Wei Sun
 
PDF
Open World Forum 2014 : From ES6 to Javascript 2.0. What use today ? par Jon...
ALTER WAY
 
PDF
Getting Comfortable with JS Promises
Asa Kusuma
 
PPTX
Asynchronous programming - .NET Way
Bishnu Rawal
 
PPTX
My final requirement
katrinaguevarra29
 
Mastering Async/Await in JavaScript
Valeri Karpov
 
Async Await for Mobile Apps
Craig Dunn
 
Async discussion 9_29_15
Cheryl Yaeger
 
How Booking.com avoids and deals with replication lag
Jean-François GagnÊ
 
Switch case and looping jam
JamaicaAubreyUnite
 
JS Fest 2018. Алексей Волков. Полезные инструменты для JS разработки
JSFestUA
 
Switch case and looping
aprilyyy
 
London SF Developers: Custom Lightning Component Error Handling
Richard Clark
 
Finding bugs in the code of LLVM project with the help of PVS-Studio
PVS-Studio
 
Compose Camp - Session3.pptx
GDSCAtharvaCollegeOf
 
Ecma script
MOHIT KUMAR
 
Task parallel library presentation
ahmed sayed
 
Avoiding callback hell with promises
TorontoNodeJS
 
Play Framework
Eduard Tudenhoefner
 
Macasu, gerrell c.
gerrell
 
Asynchronous in dot net4
Wei Sun
 
Open World Forum 2014 : From ES6 to Javascript 2.0. What use today ? par Jon...
ALTER WAY
 
Getting Comfortable with JS Promises
Asa Kusuma
 
Asynchronous programming - .NET Way
Bishnu Rawal
 
My final requirement
katrinaguevarra29
 
Ad

More from Valeri Karpov (20)

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

Recently uploaded (20)

PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Biography of Daniel Podor.pdf
Daniel Podor
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 

Introducing Async/Await