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

# await

turns Promise (future value) into a value/error.

* [JS](/web/js.md) ⟩ [async](/web/js/async.md) ⟩ await
* [JS](/web/js.md) ⟩ [statement](/web/js/grammar/statement.md) ⟩ [expression](/web/js/grammar/statement/expr.md) ⟩ [operator](/web/js/grammar/op.md) ⟩ [unary](/web/js/grammar/op/unary.md) ⟩ await

{% hint style="success" %}
([⭐️ ES2017](/web/js/feature/es2017.md)) ([primary expression](/web/js/grammar/statement/expr/primary.md)) ([unary operator](/web/js/grammar/op/unary.md))\
turns a [Promise](/web/js/async/promise.md) <mark style="color:yellow;">**/**</mark> [**thenable**](/web/js/async/await/thenable.md) into a <mark style="color:green;">**return value**</mark> <mark style="color:yellow;">**/**</mark> <mark style="color:red;">**thrown exception**</mark>.

* if <mark style="color:green;">**resolves normally**</mark>, <mark style="color:purple;">**await**</mark> <mark style="color:yellow;">**returns the result**</mark>.&#x20;
* if <mark style="color:red;">**rejected**</mark>, <mark style="color:purple;">**it**</mark> <mark style="color:red;">**throws error**</mark> (as if there were a <mark style="color:red;">`throw`</mark> statement at that line).

```javascript
// works only inside async functions❗️ 
let value = await promise;
```

{% endhint %}

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="success" %}

* <mark style="color:purple;">**await**</mark> can <mark style="color:yellow;">**only be used**</mark> <mark style="color:red;">**inside**</mark> an [async function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function):exclamation:
* modern browsers <mark style="color:green;">**allow**</mark> <mark style="color:yellow;">**top-leve**</mark>l <mark style="color:purple;">**await**</mark> in [modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules).

```javascript
// when top-level await not supported, use async IIFE instead.
(async () => {
  let value = await promise;
  // ...
})();
```

{% endhint %}
{% endtab %}

{% tab title="🔴 主題" %}

* [await promises](/web/js/async/await/promises.md) (in [parallel](/web/js/async/promise/in-parallel.md) or in [series](/web/js/async/promise/chaining.md))
  * [await in parallel](/web/js/async/await/promises/parallel.md)
  * [await sequentially](/web/js/async/await/promises/sequential.md)
* [await "thenable"](/web/js/async/await/thenable.md) - objects that <mark style="color:purple;">**await**</mark> supports.
* [error handling](/web/js/async/await/error-handling.md) for <mark style="color:purple;">**await**</mark>.
  {% endtab %}

{% tab title="📗 參考" %}

* [ ] JS.info ⟩ [async/await](https://javascript.info/async-await)
* [ ] &#x20;[JavaScript: The Definitive Guide](/web/master/ref/javascript-the-definitive-guide.md) ⟩ Ch. 13
  {% endtab %}

{% tab title="📘 手冊" %}

* [await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await)
  {% endtab %}
  {% endtabs %}
