mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-12-16 07:51:52 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -7,14 +7,14 @@
|
||||
<p>注意:如果忽略针对某个状态的回调函数参数,或者提供非函数 (nonfunction) 参数,那么 <code>then</code> 方法将会丢失关于该状态的回调函数信息,但是并不会产生错误。如果调用 <code>then</code> 的 <code>Promise</code> 的状态(fulfillment 或 rejection)发生改变,但是 <code>then</code> 中并没有关于这种状态的回调函数,那么 <code>then</code> 将创建一个没有经过回调函数处理的新 <code>Promise</code> 对象,这个新 <code>Promise</code> 只是简单地接受调用这个 <code>then</code> 的原 <code>Promise</code> 的终态作为它的终态。</p>
|
||||
</div>
|
||||
<h2 id="Syntax" name="Syntax">语法</h2>
|
||||
<pre class="syntaxbox"><var>p.then(onFulfilled, onRejected)</var>;
|
||||
<pre><code class="language-javascript"><var>p.then(onFulfilled, onRejected)</var>;
|
||||
|
||||
p.then(function(value) {
|
||||
// fulfillment
|
||||
}, function(reason) {
|
||||
// rejection
|
||||
});
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="参数">参数</h3>
|
||||
<dl>
|
||||
<dt>onFulfilled</dt>
|
||||
@@ -30,7 +30,7 @@ p.then(function(value) {
|
||||
// 'bar' 不是函数,会在内部被替换为 (x) => x
|
||||
p.then('bar').then((value) => {
|
||||
console.log(value) // 'foo'
|
||||
})</pre>
|
||||
})</code></pre>
|
||||
<p> </p>
|
||||
<dl>
|
||||
<dt>
|
||||
@@ -50,7 +50,7 @@ p.then('bar').then((value) => {
|
||||
<p>由于 <code>then</code> 和 <a href="Reference/Global_Objects/Promise/catch" title="catch() 方法返回一个Promise,并且处理拒绝的情况。它的行为与调用Promise.prototype.then(undefined, onRejected) 相同。 (事实上, calling obj.catch(onRejected) 内部calls obj.then(undefined, onRejected))."><code>Promise.prototype.catch()</code></a> 方法都会返回 promise,它们可以被链式调用 — 一种称为<strong>复合</strong>( <em>composition) </em>的操作.</p>
|
||||
<h2 id="示例">示例</h2>
|
||||
<h3 id="使用then方法"><code><font face="Open Sans, sans-serif">使用</font>then方法</code></h3>
|
||||
<pre class="brush: js">let p1 = new Promise(function(resolve, reject) {
|
||||
<pre><code class="language-javascript">let p1 = new Promise(function(resolve, reject) {
|
||||
resolve("Success!");
|
||||
// or
|
||||
// reject ("Error!");
|
||||
@@ -61,11 +61,11 @@ p1.then(function(value) {
|
||||
}, function(reason) {
|
||||
console.log(reason); // Error!
|
||||
});
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="链式调用">链式调用</h3>
|
||||
<p><font face="Open Sans, sans-serif">then 方法返回</font>一个Promise 对象,其允许方法链。</p>
|
||||
<p>你可以传递一个 lambda 给 then 并且如果它返回一个 promise,一个等价的 Promise 将暴露给后续的方法链。下面的代码片段使用 setTimout 函数来模拟异步代码操作。</p>
|
||||
<pre class="brush: js">Promise.resolve("foo")
|
||||
<pre><code class="language-javascript">Promise.resolve("foo")
|
||||
// 1. 接收 "foo" 并与 "bar" 拼接,并将其结果做为下一个resolve返回。
|
||||
.then(function(string) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
@@ -94,9 +94,9 @@ p1.then(function(value) {
|
||||
// 注意 `string` 这时不会存在 'baz'。
|
||||
// 因为这是发生在我们通过setTimeout模拟的异步函数中。
|
||||
console.log(string);
|
||||
});</pre>
|
||||
});</code></pre>
|
||||
<p>当一个值只是从一个 lambda 内部返回时,它将有效地返回 Promise.resolve(<由被调用的处理程序返回的值>)。</p>
|
||||
<pre class="brush: js">var p2 = new Promise(function(resolve, reject) {
|
||||
<pre><code class="language-javascript">var p2 = new Promise(function(resolve, reject) {
|
||||
resolve(1);
|
||||
});
|
||||
|
||||
@@ -109,9 +109,9 @@ p2.then(function(value) {
|
||||
|
||||
p2.then(function(value) {
|
||||
console.log(value); // 1
|
||||
});</pre>
|
||||
});</code></pre>
|
||||
<p>如果函数抛出错误或返回一个拒绝的Promise,则调用将返回一个拒绝的Promise。</p>
|
||||
<pre class="brush: js">Promise.resolve()
|
||||
<pre><code class="language-javascript">Promise.resolve()
|
||||
.then( () => {
|
||||
// 使 .then() 返回一个 rejected promise
|
||||
throw 'Oh no!';
|
||||
@@ -120,13 +120,13 @@ p2.then(function(value) {
|
||||
console.log( 'Not called.' );
|
||||
}, reason => {
|
||||
console.error( 'onRejected function called: ', reason );
|
||||
});</pre>
|
||||
});</code></pre>
|
||||
<p>在其他情况下,一个 resolving Promise 会被返回。在下面的例子里,第一个 then() 会返回一个用 resolving Promise 包装的 42,即使之前的 Promise 是 rejected 的。</p>
|
||||
<pre class="brush: js">Promise.reject()
|
||||
<pre><code class="language-javascript">Promise.reject()
|
||||
.then( () => 99, () => 42 ) // onRejected returns 42 which is wrapped in a resolving Promise
|
||||
.then( solution => console.log( 'Resolved with ' + solution ) ); // Resolved with 42</pre>
|
||||
.then( solution => console.log( 'Resolved with ' + solution ) ); // Resolved with 42</code></pre>
|
||||
<p>实际上,捕获 rejected promise 的需求经常大于使用 then 的两种情况语法,比如下面这样的:</p>
|
||||
<pre class="brush: js">Promise.resolve()
|
||||
<pre><code class="language-javascript">Promise.resolve()
|
||||
.then( () => {
|
||||
// 使 .then() 返回一个 rejected promise
|
||||
throw 'Oh no!';
|
||||
@@ -136,9 +136,9 @@ p2.then(function(value) {
|
||||
})
|
||||
.then( () => {
|
||||
console.log( "I am always called even if the prior then's promise rejects" );
|
||||
});</pre>
|
||||
});</code></pre>
|
||||
<p>你也可以在另一个顶层函数上使用链式去实现带有 Promise-based API 的函数。</p>
|
||||
<pre class="brush: js">function fetch_current_data() {
|
||||
<pre><code class="language-javascript">function fetch_current_data() {
|
||||
// fetch() API 返回了一个 Promise.
|
||||
// 这个函数提供了类似的API,
|
||||
// 这个函数除了实现 Promise,它还能够完成更多的工作。
|
||||
@@ -151,9 +151,9 @@ p2.then(function(value) {
|
||||
return j; // fulfillment value given to user of
|
||||
// fetch_current_data().then()
|
||||
});
|
||||
}</pre>
|
||||
}</code></pre>
|
||||
<p>如果 <code>onFulfilled</code> 返回了一个 promise,<code>then</code> 的返回值就会被 Promise resolved或者rejected。</p>
|
||||
<pre class="brush: js">function resolveLater(resolve, reject) {
|
||||
<pre><code class="language-javascript">function resolveLater(resolve, reject) {
|
||||
setTimeout(function () {
|
||||
resolve(10);
|
||||
}, 1000);
|
||||
@@ -185,7 +185,7 @@ p3.then(function(v) {
|
||||
console.log('resolved', v);
|
||||
}, function(e) {
|
||||
console.log('rejected', e); // "rejected", 20
|
||||
});</pre>
|
||||
});</code></pre>
|
||||
<h2 id="规范">规范</h2>
|
||||
<table class="standard-table">
|
||||
<tbody>
|
||||
|
||||
Reference in New Issue
Block a user