mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2026-02-28 18:11:29 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
<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 class="syntaxbox">[<em>return_value</em>] = await <em>expression</em>;</pre>
|
||||
<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>
|
||||
@@ -17,7 +17,7 @@
|
||||
<p>另外,如果 await 操作符后的表达式的值不是一个 Promise,则返回该值本身。</p>
|
||||
<h2 id="例子">例子</h2>
|
||||
<p>如果一个 Promise 被传递给一个 await 操作符,await 将等待 Promise 正常处理完成并返回其处理结果。</p>
|
||||
<pre class="brush: js">function resolveAfter2Seconds(x) {
|
||||
<pre><code class="language-javascript">function resolveAfter2Seconds(x) {
|
||||
return new Promise(resolve => {
|
||||
setTimeout(() => {
|
||||
resolve(x);
|
||||
@@ -31,23 +31,23 @@ async function f1() {
|
||||
}
|
||||
f1();
|
||||
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>如果该值不是一个 Promise,await 会把该值转换为已正常处理的Promise,然后等待其处理结果。</p>
|
||||
<pre class="brush: js">async function f2() {
|
||||
<pre><code class="language-javascript">async function f2() {
|
||||
var y = await 20;
|
||||
console.log(y); // 20
|
||||
}
|
||||
f2();
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>如果 Promise 处理异常,则异常值被抛出。</p>
|
||||
<pre class="brush: js">async function f3() {
|
||||
<pre><code class="language-javascript">async function f3() {
|
||||
try {
|
||||
var z = await Promise.reject(30);
|
||||
} catch (e) {
|
||||
console.log(e); // 30
|
||||
}
|
||||
}
|
||||
f3();</pre>
|
||||
f3();</code></pre>
|
||||
<h2 id="规范">规范</h2>
|
||||
<table class="standard-table">
|
||||
<thead>
|
||||
|
||||
Reference in New Issue
Block a user