Async/Await is a syntax that lets you write Promises more clearly, reading almost like synchronous code.
• async: declares that a function returns a promise
• await: waits until a promise resolves (usable only inside an async function)
javascript
function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => resolve('resolved'), 2000);
});
}
async function asyncCall() {
console.log('calling');
const result = await resolveAfter2Seconds();
console.log(result); // "resolved"
}
asyncCall();You should see
calling (2 seconds later) resolved