Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Advanced JavaScript

You're reading from   Advanced JavaScript Speed up web development with the powerful features and benefits of JavaScript

Arrow left icon
Product type Paperback
Published in Jan 2019
Publisher
ISBN-13 9781789800104
Length 330 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Zachary Shute Zachary Shute
Author Profile Icon Zachary Shute
Zachary Shute
Arrow right icon
View More author details
Toc

Async/Await


Async/await is a new syntax form added to simplify code that uses promises. Async/await introduces two new keywords: async and await. Async is added to function declarations and await is used inside an async function. It is surprisingly easy to understand and use. In its simplest form, async/await allows us to write promise-based asynchronous code that looks almost identical to the synchronous code that does the same task. We will use async/await to simplify code using promises and make it even easier to read and understand.

Async/Await Syntax

The async keyword is added to function declarations; it must precede the function keyword. An async function declaration defines an asynchronous function. An example declaration of an async function is shown in the following snippet:

async function asyncExample( /* arguments */  ){ /* do work */ }

Snippet 2.34: Implementing promises

An async function implicitly returns a promise, no matter what the return value is specified to be. If the return...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime