# How do I use async and await?

```javascript
// A list of promises that pass using await and async
async function f () {
  try {
    const a = await Promise.resolve(1)
    const b = await Promise.resolve(2)
    console.log(`[case1] pass: ${a + b}`)
  } catch (error) {
    console.log(`[case1] fail: ${error.toString()}`)
  }

}

f()
```

```javascript
// A list of promises that fail using await and async
async function g () {
  try {
    const a = await Promise.resolve(1)
    const b = await Promise.reject(2)
    console.log(`[case2] pass: ${a + b}`)
  } catch (error) {
    console.log(`[case2] fail: ${error.toString()}`)
  }

}

g()
```

```javascript
// The return value of an async function is wrapped in a promise
const getMessage = async () => 'hello'
getMessage().then((message) => console.log(message))
```

```javascript
// How can I make async requests in parallel fashion (Faster)

// Approach 1: Requests are made in parallel (despite looking like they would run serially!)
async function getMovieDetails(){
  const request1 = $.getJSON('https://omdbapi.com?t=temple&apikey=thewdb')
  const request2 = $.getJSON('https://omdbapi.com?t=tide&apikey=thewdb')
  const details1 = await request1
  const details2 = await request2
  return [details1, details2]  
}

getMovieDetails().then(detailsList => detailsList.map(detail => console.log(detail)))


// Approach 2: requests are also made in parallel
async function getMovieDetails(){
  const keywordList = ['temple', 'tide']
  const addressList = keywordList.map(t => `https://omdbapi.com?t=${t}&apikey=thewdb`)
  const requestList = addressList.map(request => $.getJSON(request))
  const detailsList = await Promise.all(requestList)
  return detailsList
}

getMovieDetails().then(detailsList => detailsList.map(detail => console.log(detail)))
```

```javascript
// How can I make async requests in serial fashion (Slower)
async function getMovieDetails(){
  const details1 = await $.getJSON('https://omdbapi.com?t=temple&apikey=thewdb')
  const details2 = await $.getJSON('https://omdbapi.com?t=tide&apikey=thewdb')  
  return [details1, details2]
}

getMovieDetails().then(detailsList => detailsList.map(detail => console.log(detail)))
```


---

# Agent Instructions: 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://asad-razvi.gitbook.io/javascript-cookbook/asynchronous-recipes/how-do-i-use-async-and-await.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.
