{
"name": "egghead-async-await",
"version": "1.0.0",
"author": "Marius Schulz",
"license": "MIT",
"dependencies": {
"bluebird": "^3.5.0",
"node-fetch": "^1.6.3"
},
"scripts": {
"build:8": "tsc -p ./lesson-8"
},
"devDependencies": {
"typescript": "^2.3.0-dev.20170404"
}
}
const fetch = require("node-fetch");
async function showGitHubUser(handle) {
const url = `https://api.github.com/users/${handle}`;
const response = await fetch(url);
const user = await response.json();
console.log(user.name);
console.log(user.location);
}
showGitHubUser("phamngoctuong");
const fetch = require("node-fetch");
async function showGitHubUser(handle) {
const url = `https://api.github.com/users/${handle}`;
const response = await fetch(url);
return await response.json();
}
showGitHubUser("phamngoctuong").then(user => {
console.log(user.name);
console.log(user.location);
});
const fetch = require("node-fetch");
class GitHubApiClient {
async fetchUser(handle) {
const url = `https://api.github.com/users/${handle}`;
const response = await fetch(url);
return await response.json();
}
}
(async () => {
const client = new GitHubApiClient();
const user = await client.fetchUser("phamngoctuong");
console.log(user.name);
console.log(user.location);
})();
const fetch = require("node-fetch");
async function fetchFromGitHub(endpoint) {
const url = `https://api.github.com${endpoint}`;
const response = await fetch(url);
const json = await response.json();
if (response.status != 200) {
throw Error(json.message);
}
return json;
}
async function showGitHubUser(handle) {
try {
const user = await fetchFromGitHub(`/users/${handle}`);
console.log(user.name);
console.log(user.location);
} catch (error) {
console.error(`Error: ${error.message}`);
}
}
showGitHubUser("phamngoctuong-sai");
const fetch = require("node-fetch");
async function fetchFromGitHub(endpoint) {
const url = `https://api.github.com${endpoint}`;
const response = await fetch(url);
return await response.json();
}
async function showUserAndRepos(handle) {
const userPromise = fetchFromGitHub(`/users/${handle}`);
const reposPromise = fetchFromGitHub(`/users/${handle}/repos`);
const user = await userPromise;
const repos = await reposPromise;
console.log(user.name);
console.log(`${repos.length} repos`);
}
showUserAndRepos("phamngoctuong");
const fetch = require("node-fetch");
async function fetchFromGitHub(endpoint) {
const url = `https://api.github.com${endpoint}`;
const response = await fetch(url);
return await response.json();
}
async function fetchUserAndRepos(handle) {
const [user, repos] = await Promise.all([fetchFromGitHub(`/users/${handle}`), fetchFromGitHub(`/users/${handle}/repos`)]);
console.log(user.name);
console.log(`${repos.length} repos`);
}
fetchUserAndRepos("phamngoctuong");
Trong JavaScript, thenable là một đối tượng có hàm then(). => Mọi lời hứa đều là thenable, nhưng không phải mọi điều tốt đẹp đều là promises.
const Bluebird = require("bluebird");
async function main() {
console.log("Working ...");
await Bluebird.delay(2000);
console.log("Done.");
}
main();
Symbol.asyncIterator = Symbol.asyncIterator || Symbol("asyncIterator");
const delay = (ms) => new Promise(resolve => {
setTimeout(resolve, ms);
});
async function* someGenerator() {
await delay(1000);
yield 1;
await delay(1000);
yield 2;
await delay(1000);
yield 3;
}
async function main() {
for await (const value of someGenerator()) {
console.log(value);
}
}
main();