Async/Await Javascript Full Example (ok)
https://github.com/mariusschulz/egghead-async-await
Lession 1:
package.json
{
  "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"
  }
}async.js trả giá trị bên trong
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");
Lesson 2:
async.js trả giá trị bên ngoài
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);
});
Lesson 3:
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);
})();
Lesson 4:
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");
Lesson 5:
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");
Lesson 6:
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");
Lesson 7:
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();
Lesson 8:
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();
PreviousCheck if Function Exists before Calling? (ok)NextThenables in JavaScript phục vụ cho bài viết trên (ok)
Last updated
Was this helpful?

