Skip to main content Accessibility Feedback

Ajax & APIs

async & await

When you use the async operator before a function, you turn it into an async function.

Inside an async function, you can use the await operator before asynchronous code to tell the function to wait for that operation to complete before moving on.

To handle errors, wrap your code in a try...catch().

async function getAPIData () {
	try {
		let request = await fetch('');
		if (!request.ok) throw request;
		let response = await request.json();
	} catch (err) {


Note: an async function always returns a promise, even if you’re not actually making any asynchronous calls in it.