Async and Await
Explore how async and await in JavaScript enable writing clearer asynchronous code for game development. Discover how to handle promises, fetch data, and manage errors efficiently to support smooth gameplay features like server-side high scores.
We'll cover the following...
Async functions and the await keyword, new additions in ECMAScript 2017, act as syntactic sugar on top of promises. They allow us to write synchronous-looking code while performing asynchronous tasks behind the scenes.
Async
First, we have the async keyword. We put it in front of a function declaration to turn it into an async function.
async function getData(url) {}
Invoking the function now returns a promise. This is one of the traits of async functions; their return values are converted to promises.
Async functions enable us to write promise-based code as if it were synchronous, without blocking the execution thread and instead operating ...