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
Asynchronous javascript
PPTX
Express js
PDF
Asynchronous JavaScript Programming
PPTX
Introduction Node.js
PDF
React and redux
PDF
An Introduction of Node Package Manager (NPM)
PDF
Introduction to RxJS
PPTX
ReactJS presentation.pptx
Asynchronous javascript
Express js
Asynchronous JavaScript Programming
Introduction Node.js
React and redux
An Introduction of Node Package Manager (NPM)
Introduction to RxJS
ReactJS presentation.pptx

What's hot (20)

PPTX
Node js introduction
PDF
Angular Advanced Routing
PDF
VueJS Introduction
PPTX
React Hooks
PPTX
Introduction to angular with a simple but complete project
PPTX
React JS: A Secret Preview
PDF
Basics of React Hooks.pptx.pdf
ODP
Basics of VueJS
PDF
JavaScript Promises
PPTX
Its time to React.js
PPTX
PDF
Important React Hooks
PPTX
React JS - A quick introduction tutorial
PDF
React JS & Functional Programming Principles
PDF
PPTX
Top 10 RxJs Operators in Angular
PPTX
Understanding react hooks
PDF
Angular Observables & RxJS Introduction
PPTX
Angular interview questions
PPT
TypeScript Presentation
Node js introduction
Angular Advanced Routing
VueJS Introduction
React Hooks
Introduction to angular with a simple but complete project
React JS: A Secret Preview
Basics of React Hooks.pptx.pdf
Basics of VueJS
JavaScript Promises
Its time to React.js
Important React Hooks
React JS - A quick introduction tutorial
React JS & Functional Programming Principles
Top 10 RxJs Operators in Angular
Understanding react hooks
Angular Observables & RxJS Introduction
Angular interview questions
TypeScript Presentation
Ad

Similar to Introducing Async/Await (20)

PDF
Mastering Async/Await in JavaScript
PDF
Asynchronous JavaScript Programming with Callbacks & Promises
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
Mastering Async/Await in JavaScript
Asynchronous JavaScript Programming with Callbacks & Promises
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
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)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Machine learning based COVID-19 study performance prediction
PDF
cuic standard and advanced reporting.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Approach and Philosophy of On baking technology
PDF
Encapsulation theory and applications.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Electronic commerce courselecture one. Pdf
20250228 LYD VKU AI Blended-Learning.pptx
sap open course for s4hana steps from ECC to s4
Machine learning based COVID-19 study performance prediction
cuic standard and advanced reporting.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
MYSQL Presentation for SQL database connectivity
Understanding_Digital_Forensics_Presentation.pptx
Empathic Computing: Creating Shared Understanding
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Mobile App Security Testing_ A Comprehensive Guide.pdf
Unlocking AI with Model Context Protocol (MCP)
Advanced methodologies resolving dimensionality complications for autism neur...
Approach and Philosophy of On baking technology
Encapsulation theory and applications.pdf
Network Security Unit 5.pdf for BCA BBA.
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
NewMind AI Weekly Chronicles - August'25 Week I
Diabetes mellitus diagnosis method based random forest with bat algorithm
Per capita expenditure prediction using model stacking based on satellite ima...
Electronic commerce courselecture one. Pdf

Introducing Async/Await