语法高亮,滚动条美化,设置页面调整

This commit is contained in:
fofolee
2019-04-19 02:41:09 +08:00
parent 1e8f76c000
commit 359d29ee0b
1590 changed files with 12328 additions and 11441 deletions

View File

@@ -7,8 +7,8 @@
<div><iframe class="interactive interactive-js taller" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/statement-async.html" width="100%"></iframe></div>
<p class="hidden">该交互式demo源文件存储于Github仓库中。如果希望为此交互式项目做出贡献,请 clone <a class="external" href="https://github.com/mdn/interactive-examples" rel="noopener">https://github.com/mdn/interactive-examples</a> 项目并用pull形式向我们的原始仓库发出请求。</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">async function <em>name</em>([<em>param</em>[, <em>param</em>[, ... <em>param</em>]]]) { <em>statements </em>}
</pre>
<pre><code class="language-javascript">async function <em>name</em>([<em>param</em>[, <em>param</em>[, ... <em>param</em>]]]) { <em>statements </em>}
</code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>name</code></dt>
@@ -35,7 +35,7 @@
</div>
<h2 id="示例">示例</h2>
<h3 id="简单例子">简单例子</h3>
<pre class="brush: js">var resolveAfter2Seconds = function() {
<pre><code class="language-javascript">var resolveAfter2Seconds = function() {
  console.log("starting slow promise");
  return new Promise(resolve =&gt; {
    setTimeout(function() {
@@ -114,7 +114,7 @@ setTimeout(parallel, 10000); // truly parallel: after 1 second, logs "fast", the
// wait again
setTimeout(parallelPromise, 13000); // same as parallel
</pre>
</code></pre>
<div class="note">
<h4 id="await_and_parallelism"><code>await</code> and parallelism</h4>
<p><code>sequentialStart</code>中,程序在第一个<code>await</code>停留了2秒然后又在第二个<code>await</code>停留了1秒。直到第一个计时器结束后第二个计时器才被创建。程序是3秒后才结束。</p>
@@ -136,7 +136,7 @@ setTimeout(parallelPromise, 13000); // same as parallel
</div>
<h3 id="通过async函数重写_promise_链">通过<code>async</code>函数重写 promise 链</h3>
<p>返回 <a href="Reference/Global_Objects/Promise" title="Promise 对象用于表示一个异步操作的最终状态完成或失败以及该异步操作的结果值。"><code>Promise</code></a>的 API 将会被用于 promise 链,它会将函数分成若干部分。例如下面代码:</p>
<pre class="brush: js">function getProcessedData(url) {
<pre><code class="language-javascript">function getProcessedData(url) {
return downloadData(url) // 返回一个 promise 对象
.catch(e =&gt; {
return downloadFallbackData(url) // 返回一个 promise 对象
@@ -144,9 +144,9 @@ setTimeout(parallelPromise, 13000); // same as parallel
.then(v =&gt; {
return processDataInWorker(v); // 返回一个 promise 对象
});
}</pre>
}</code></pre>
<p>可以通过如下所示的一个<code>async</code>函数重写:</p>
<pre class="brush: js">async function getProcessedData(url) {
<pre><code class="language-javascript">async function getProcessedData(url) {
let v;
try {
v = await downloadData(url);
@@ -155,7 +155,7 @@ setTimeout(parallelPromise, 13000); // same as parallel
}
return processDataInWorker(v);
}
</pre>
</code></pre>
<p>注意,在上述示例中,<code>return</code> 语句中没有 <code>await</code> 操作符,因为 <code>async function</code> 的返回值将被隐式地传递给 <code><a href="Reference/Global_Objects/Promise/resolve" title="The source for this interactive demo is stored in a GitHub repository. If you'd like to contribute to the interactive demo project, please clone https://github.com/mdn/interactive-examples and send us a pull request."><code>Promise.resolve</code></a></code></p>
<h2 id="规范">规范</h2>
<table class="standard-table">