Asynchronous functions are a TypeScript feature that arrived with the TypeScript 1.6 release. Developers can use the await keyword to wait for an asynchronous operation to be completed 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 asynchronous functions.
Let's take a look at a basic async/await example:
let p = Promise.resolve(3);
async function fn(): Promise<number> {
var 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 represents a future result. As we can see, the fn function is preceded by the async keyword...