Skip to Content
Learning TypeScript 2.x - Second Edition
book

Learning TypeScript 2.x - Second Edition

by Remo H. Jansen
April 2018
Beginner content levelBeginner
536 pages
13h 21m
English
Packt Publishing
Content preview from Learning TypeScript 2.x - Second Edition

Asynchronous functions – async and await

Asynchronous functions are a TypeScript feature that arrived with the TypeScript 1.6 release. Developers can use the await keyword to wait for the function results without blocking the normal execution of the program.

Using asynchronous functions helps to increase the readability of a piece of code when compared with the use of promises or callbacks, but technically, we can achieve the same features using both promises and synchronous code.

Let's take a look at a basic async/await example:

let p = Promise.resolve(3); 
 
async function fn(): Promise<number> { 
    let i = await p; // 3 
    return 1 + i; // 4 
} 
 
fn().then((r) => console.log(r)); // 4 

The preceding code snippet declares a promise named p. This promise ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering TypeScript - Fourth Edition

Mastering TypeScript - Fourth Edition

Nathan Rozentals
Learning TypeScript

Learning TypeScript

Josh Goldberg
TypeScript for Beginners

TypeScript for Beginners

Bharath Thippireddy

Publisher Resources

ISBN: 9781788391474Supplemental Content