> For the complete documentation index, see [llms.txt](https://choubey.gitbook.io/javascript-interview-questions/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://choubey.gitbook.io/javascript-interview-questions/question-25.md).

# Question 25

### Question

Explain how to use `async/await` with non-Promise values.

### Answer

You can't directly use `await` on a regular value (like a number or string).

Here's how you handle situations where you need to work with non-Promise values in an `async` context:

**1. Converting to a Promise:**

The most common approach is to wrap the non-Promise value into a resolved Promise. This signals that the operation is complete and the value is available.

```javascript
async function myAsyncFunction() {
  const someValue = 42; // Non-Promise value

  // Wrap it in a promise that immediately resolves
  const promiseValue = new Promise(resolve => resolve(someValue));

  const result = await promiseValue; 
  console.log(result); // Output: 42
}

myAsyncFunction(); 
```

**Explanation:**

* We create a `Promise` that immediately resolves with the `someValue`. This fulfills the Promise, making it behave like an asynchronous operation.
* `await` can then be used on this resolved Promise to access the value asynchronously.

**2. Handling Synchronous Operations:**

If the non-Promise value is generated synchronously (e.g., a calculation), you might not need `async/await`.

```javascript
function myFunction() {
  const someValue = 42; // Non-Promise value
  console.log(someValue); // Outputs immediately
}

myFunction(); // Executes immediately
```

**Important Considerations:**

* **Purpose of `async/await`:** Remember that `async/await` is primarily designed for handling asynchronous operations where results might take time to become available (like network requests, file I/O).
* If you are simply working with synchronous data, there's often no need to wrap it in Promises. Using Promises unnecessarily can add complexity to your code without any performance benefit.


---

# 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://choubey.gitbook.io/javascript-interview-questions/question-25.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.
