use async await in class component code example
Example 1: async await class component react
async componentDidMount() {
const response = await fetch('https://jsonplaceholder.typicode.com/users');
const json = await response.json();
console.log(json);
}
Example 2: async
function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
async function asyncCall() {
console.log('calling');
const result = await resolveAfter2Seconds();
console.log(result);
}
asyncCall();