Created
November 29, 2019 05:52
-
-
Save prashant1k99/cec9742267f5d6ab09c5ed840f5213cc to your computer and use it in GitHub Desktop.
Async Await
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function f() { | |
return 1; | |
} | |
f().then(alert); // 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function f() { | |
return Promise.resolve(1); | |
} | |
f().then(alert); // 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function f() { | |
let promise = new Promise((resolve, reject) => { | |
setTimeout(() => resolve("done!"), 1000) | |
}); | |
let result = await promise; // wait until the promise resolves (*) | |
alert(result); // "done!" | |
} | |
// Await can only be used when the async is used | |
f(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function showAvatar() { | |
// read our JSON | |
let response = await fetch('/article/promise-chaining/user.json'); | |
let user = await response.json(); | |
// read github user | |
let githubResponse = await fetch(`https://api.github.com/users/${user.name}`); | |
let githubUser = await githubResponse.json(); | |
// show the avatar | |
let img = document.createElement('img'); | |
img.src = githubUser.avatar_url; | |
img.className = "promise-avatar-example"; | |
document.body.append(img); | |
// wait 3 seconds | |
await new Promise((resolve, reject) => setTimeout(resolve, 3000)); | |
img.remove(); | |
return githubUser; | |
} | |
showAvatar(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HttpError extends Error { | |
constructor(response) { | |
super(`${response.status} for ${response.url}`); | |
this.name = 'HttpError'; | |
this.response = response; | |
} | |
} | |
function loadJson(url) { | |
return fetch(url) | |
.then(response => { | |
if (response.status == 200) { | |
return response.json(); | |
} else { | |
throw new HttpError(response); | |
} | |
}) | |
} | |
// Ask for a user name until github returns a valid user | |
function demoGithubUser() { | |
let name = prompt("Enter a name?", "iliakan"); | |
return loadJson(`https://api.github.com/users/${name}`) | |
.then(user => { | |
alert(`Full name: ${user.name}.`); | |
return user; | |
}) | |
.catch(err => { | |
if (err instanceof HttpError && err.response.status == 404) { | |
alert("No such user, please reenter."); | |
return demoGithubUser(); | |
} else { | |
throw err; | |
} | |
}); | |
} | |
demoGithubUser(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment