> For the complete documentation index, see [llms.txt](https://javascriptuse.gitbook.io/javascript/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://javascriptuse.gitbook.io/javascript/async-await-javascript-full-example-ok.md).

# Async/Await Javascript Full Example (ok)

Lession 1:&#x20;

1. [Write an Asynchronous Function with async/await](https://github.com/mariusschulz/egghead-async-await/tree/master/lesson-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");
```

![](/files/bObsBufAPPrdLkNQ3md2)

Lesson 2:&#x20;

1. [Call an Asynchronous Function in a Promise Chain](https://github.com/mariusschulz/egghead-async-await/tree/master/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);
});
```

![](/files/4Ty0zHHhiszbGK9lqMDL)

Lesson 3:

1. [Convert Any Function into an Asynchronous Function](https://github.com/mariusschulz/egghead-async-await/tree/master/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);
})();
```

![](/files/a9dnfw3SqGNWIf2sMEmn)

Lesson 4:&#x20;

1. [Handle Errors in Asynchronous Functions](https://github.com/mariusschulz/egghead-async-await/tree/master/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");
```

![](/files/4l6At1WhOUi9oeRlPhAL)

Lesson 5:&#x20;

1. [Await Multiple Promises Sequentially or Concurrently](https://github.com/mariusschulz/egghead-async-await/tree/master/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");
```

![](/files/g8d5BDoKfF9gXb3NXsMU)

Lesson 6:&#x20;

1. [Await Multiple Promises Concurrently with Promise.all()](https://github.com/mariusschulz/egghead-async-await/tree/master/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");
```

![](/files/LmucDn811SSRnk2Gx1DN)

Lesson 7:&#x20;

1. [Use the await Operator with Any Thenable](https://github.com/mariusschulz/egghead-async-await/tree/master/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.&#x20;

```
const Bluebird = require("bluebird");
async function main() {
  console.log("Working ...");
  await Bluebird.delay(2000);
  console.log("Done.");
}
main();
```

![](/files/DHV2bA3HACu48MObe6D3)

{% embed url="<https://app.gitbook.com/o/-LYjAG1--qK4zFZOXpzU/s/-LYjAG13rAPdbArSfRyB/thenables-in-javascript-phuc-vu-cho-bai-viet-tren-ok>" %}

Lesson 8:&#x20;

1. [Iterate Asynchronously with the for-await-of Loop](https://github.com/mariusschulz/egghead-async-await/tree/master/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();
```

![](/files/l1lsGPhVTre83hcHuHbJ)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://javascriptuse.gitbook.io/javascript/async-await-javascript-full-example-ok.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
