js example async await function
Example 1: async await
async function showAvatar() {
let response = await fetch('/article/promise-chaining/user.json');
let user = await response.json();
let githubResponse = await fetch(`https://api.github.com/users/${user.name}`);
let githubUser = await githubResponse.json();
let img = document.createElement('img');
img.src = githubUser.avatar_url;
img.className = "promise-avatar-example";
document.body.append(img);
await new Promise((resolve, reject) => setTimeout(resolve, 3000));
img.remove();
return githubUser;
}
showAvatar();
Example 2: js async await
async function myFunction(){
await ...
}
const myFunction2 = async () => {
await ...
}
const obj = {
async getName() {
return fetch('https://www.example.com');
}
}
class Obj {
async getResource {
return fetch('https://www.example.com');
}
}