2019-04-21 11:50:48 +08:00

136 lines
7.1 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<p><code>await</code>  操作符用于等待一个<a href="Reference/Global_Objects/Promise" title="Promise 对象用于表示一个异步操作的最终状态完成或失败以及该异步操作的结果值。"><code>Promise</code></a> 对象。它只能在异步函数 <a href="Reference/Statements/async_function" title="async function 声明用于定义一个返回 AsyncFunction 对象的异步函数。异步函数是指通过事件循环异步执行的函数,它会通过一个隐式的 Promise 返回其结果。但是如果你的代码使用了异步函数,它的语法和结构会更像是标准的同步函数。"><code>async function</code></a> 中使用。</p>
<h2 id="语法">语法</h2>
<pre><code class="language-javascript">[<em>return_value</em>] = await <em>expression</em>;</code></pre>
<dl>
<dt><font face="Consolas, Liberation Mono, Courier, monospace">表达式</font></dt>
<dd>一个 <a href="Reference/Global_Objects/Promise" title="Promise 对象用于表示一个异步操作的最终状态完成或失败以及该异步操作的结果值。"><code>Promise</code></a> 对象或者任何要等待的值。</dd>
<dt><font face="Consolas, Liberation Mono, Courier, monospace">返回值</font></dt>
<dd>
<p>返回 Promise 对象的处理结果。如果等待的不是 Promise 对象,则返回该值本身。</p>
</dd>
</dl>
<h2 id="描述">描述</h2>
<p>await 表达式会暂停当前 <a href="Reference/Statements/async_function" title="async function 声明用于定义一个返回 AsyncFunction 对象的异步函数。异步函数是指通过事件循环异步执行的函数,它会通过一个隐式的 Promise 返回其结果。但是如果你的代码使用了异步函数,它的语法和结构会更像是标准的同步函数。"><code>async function</code></a> 的执行,等待 Promise 处理完成。若 Promise 正常处理(fulfilled)其回调的resolve函数参数作为 await 表达式的值,继续执行 <a href="Reference/Statements/async_function" title="async function 声明用于定义一个返回 AsyncFunction 对象的异步函数。异步函数是指通过事件循环异步执行的函数,它会通过一个隐式的 Promise 返回其结果。但是如果你的代码使用了异步函数,它的语法和结构会更像是标准的同步函数。"><code>async function</code></a></p>
<p>若 Promise 处理异常(rejected)await 表达式会把 Promise 的异常原因抛出。</p>
<p>另外,如果 await 操作符后的表达式的值不是一个 Promise则返回该值本身。</p>
<h2 id="例子">例子</h2>
<p>如果一个 Promise 被传递给一个 await 操作符await 将等待 Promise 正常处理完成并返回其处理结果。</p>
<pre><code class="language-javascript">function resolveAfter2Seconds(x) {
return new Promise(resolve =&gt; {
setTimeout(() =&gt; {
resolve(x);
}, 2000);
});
}
async function f1() {
var x = await resolveAfter2Seconds(10);
console.log(x); // 10
}
f1();
</code></pre>
<p>如果该值不是一个 Promiseawait 会把该值转换为已正常处理的Promise然后等待其处理结果。</p>
<pre><code class="language-javascript">async function f2() {
var y = await 20;
console.log(y); // 20
}
f2();
</code></pre>
<p>如果 Promise 处理异常,则异常值被抛出。</p>
<pre><code class="language-javascript">async function f3() {
try {
var z = await Promise.reject(30);
} catch (e) {
console.log(e); // 30
}
}
f3();</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td><a class="external" href="https://tc39.github.io/ecmascript-asyncawait/#async-function-definitions" hreflang="en" lang="en" rel="noopener">ECMAScript Async Functions<br/><small lang="zh-CN">async function</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td>提案</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div></div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Edge</th>
<th>Opera</th>
<th>Safari (WebKit)</th>
</tr>
<tr>
<td>基本支持</td>
<td>55</td>
<td><a href="/en-US/Firefox/Releases/52" title="Released on 2017-03-07.">52.0</a> (52.0)</td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td>42</td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Android Webview</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
<th>Chrome for Android</th>
</tr>
<tr>
<td> 基本支持</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td>52.0 (52.0)</td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td>42</td>
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
<td>55</td>
</tr>
</tbody>
</table>
</div>
<h2 id="查看更多">查看更多</h2>
<ul>
<li><a href="Reference/Statements/async_function" title="async function 声明用于定义一个返回 AsyncFunction 对象的异步函数。异步函数是指通过事件循环异步执行的函数,它会通过一个隐式的 Promise 返回其结果。但是如果你的代码使用了异步函数,它的语法和结构会更像是标准的同步函数。"><code>async 函数</code></a></li>
<li><a class="new" href="Reference/Operators/async_function" rel="nofollow" title="此页面仍未被本地化, 期待您的翻译!"><code>async 函数表达式</code></a></li>
<li><a href="Reference/Global_Objects/AsyncFunction" title="AsyncFunction 构造函数用来创建新的 异步函数 对象JavaScript 中每个异步函数都是  AsyncFunction 的对象。"><code>AsyncFunction</code></a> object</li>
</ul>
</article>