> 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-javascript-callback-promises-async-await-video-ok.md).

# ASYNC JavaScript (CALLBACK, PROMISES, ASYNC AWAIT) video (ok)

```
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Async</title>
</head>

<body>
  <img id="pic1"></img>
  <img id="pic2"></img>
  <img id="pic3"></img>
  <script type="text/javascript">
  function httpGetAsync(theUrl, callback) {
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() { //Call a function when the state changes.
      if (xmlHttp.readyState == 4 && xmlHttp.status == 200) callback(xmlHttp);
    }
    xmlHttp.open('GET', theUrl, true); // true for asynchronous
    xmlHttp.send(null);
  }
  // ---------------------- Part 1 ---------------------- 
  // httpGetAsync('https://loremflickr.com/320/240',(data)=>{
  //   document.getElementById('pic1').setAttribute('src',data.responseURL);
  //   httpGetAsync('https://loremflickr.com/320/240',(data)=>{
  //     document.getElementById('pic2').setAttribute('src',data.responseURL);
  //     httpGetAsync('https://loremflickr.com/320/240',(data)=>{
  //       document.getElementById('pic3').setAttribute('src',data.responseURL);
  //     });
  //   });
  // });
  // ---------------------- Part 2 ---------------------- 
  const currentPromise1 = new Promise((resolve, reject) => {
    httpGetAsync('https://loremflickr.com/320/240', resolve);
  });
  const currentPromise2 = new Promise((resolve, reject) => {
    httpGetAsync('https://loremflickr.com/320/240', resolve);
  });
  const currentPromise3 = new Promise((resolve, reject) => {
    httpGetAsync('https://loremflickr.com/320/240', resolve);
  });
  // currentPromise1
  //   .then((data) => {
  //     document.getElementById('pic1').setAttribute('src', data.responseURL);
  //   }).catch((error) => {
  //     console.log(error);
  //   });
  // currentPromise2
  //   .then((data) => {
  //     document.getElementById('pic2').setAttribute('src', data.responseURL);
  //   }).catch((error) => {
  //     console.log(error);
  //   });
  // currentPromise3
  //   .then((data) => {
  //     document.getElementById('pic3').setAttribute('src', data.responseURL);
  //   }).catch((error) => {
  //     console.log(error);
  //   });
  // ---------------------- Part 3 ---------------------- 
  const execute = async () => {
    try {
      const response1 = await currentPromise1;
      document.getElementById('pic1').setAttribute('src', response1.responseURL);
      const response2 = await currentPromise2;
      document.getElementById('pic2').setAttribute('src', response2.responseURL);
      const response3 = await currentPromise3;
      document.getElementById('pic3').setAttribute('src', response3.responseURL);
    } catch (error) {
      console.log("{0} exception caught.", error);
    }
  }
  execute();
  </script>
</body>

</html>
```


---

# 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-javascript-callback-promises-async-await-video-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.
