mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-12-20 02:05:48 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
<div><iframe class="interactive interactive-js" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/promise-all.html" width="100%"></iframe></div>
|
||||
<p class="hidden">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 <a class="external" href="https://github.com/mdn/interactive-examples" rel="noopener">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p>
|
||||
<h2 id="语法">语法</h2>
|
||||
<pre class="syntaxbox"><var>Promise.all(iterable)</var>;</pre>
|
||||
<pre><code class="language-javascript"><var>Promise.all(iterable)</var>;</code></pre>
|
||||
<h3 id="参数">参数</h3>
|
||||
<dl>
|
||||
<dt>iterable</dt>
|
||||
@@ -27,7 +27,7 @@
|
||||
<h2 id="示例">示例</h2>
|
||||
<h3 id="Promise.all_的使用"><code>Promise.all</code> 的使用</h3>
|
||||
<p><code>Promise.all</code> 等待所有都完成(或第一个失败)。</p>
|
||||
<pre class="brush: js">var p1 = Promise.resolve(3);
|
||||
<pre><code class="language-javascript">var p1 = Promise.resolve(3);
|
||||
var p2 = 1337;
|
||||
var p3 = new Promise((resolve, reject) => {
|
||||
setTimeout(resolve, 100, 'foo');
|
||||
@@ -35,9 +35,9 @@ var p3 = new Promise((resolve, reject) => {
|
||||
|
||||
Promise.all([p1, p2, p3]).then(values => {
|
||||
console.log(values); // [3, 1337, "foo"]
|
||||
});</pre>
|
||||
});</code></pre>
|
||||
<p>如果参数中包含非 <code>promise</code> 值,这些值将被忽略,但仍然会被放在返回数组中(如果 <code>promise</code> 完成的话):</p>
|
||||
<pre class="brush: js">// this will be counted as if the iterable passed is empty, so it gets fulfilled
|
||||
<pre><code class="language-javascript">// this will be counted as if the iterable passed is empty, so it gets fulfilled
|
||||
var p = Promise.all([1,2,3]);
|
||||
// this will be counted as if the iterable passed contains only the resolved promise with value "444", so it gets fulfilled
|
||||
var p2 = Promise.all([1,2,3, Promise.resolve(444)]);
|
||||
@@ -54,10 +54,10 @@ setTimeout(function(){
|
||||
// logs
|
||||
// Promise { <state>: "fulfilled", <value>: Array[3] }
|
||||
// Promise { <state>: "fulfilled", <value>: Array[4] }
|
||||
// Promise { <state>: "rejected", <reason>: 555 }</pre>
|
||||
// Promise { <state>: "rejected", <reason>: 555 }</code></pre>
|
||||
<h3 id="Promise.all_的异步和同步"><code>Promise.all</code> 的异步和同步</h3>
|
||||
<p>下面的例子中演示了 <code>Promise.all</code> 的异步性(如果传入的可迭代对象是空的,就是同步):</p>
|
||||
<pre class="brush: js">// we are passing as argument an array of promises that are already resolved,
|
||||
<pre><code class="language-javascript">// we are passing as argument an array of promises that are already resolved,
|
||||
// to trigger Promise.all as soon as possible
|
||||
var resolvedPromisesArray = [Promise.resolve(33), Promise.resolve(44)];
|
||||
|
||||
@@ -75,9 +75,9 @@ setTimeout(function(){
|
||||
// Promise { <state>: "pending" }
|
||||
// the stack is now empty
|
||||
// Promise { <state>: "fulfilled", <value>: Array[2] }
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>如果 <code>Promise.all</code> 失败,也是一样的:</p>
|
||||
<pre class="brush: js">var mixedPromisesArray = [Promise.resolve(33), Promise.reject(44)];
|
||||
<pre><code class="language-javascript">var mixedPromisesArray = [Promise.resolve(33), Promise.reject(44)];
|
||||
var p = Promise.all(mixedPromisesArray);
|
||||
console.log(p);
|
||||
setTimeout(function(){
|
||||
@@ -89,9 +89,9 @@ setTimeout(function(){
|
||||
// Promise { <state>: "pending" }
|
||||
// the stack is now empty
|
||||
// Promise { <state>: "rejected", <reason>: 44 }
|
||||
</pre>
|
||||
</code></pre>
|
||||
<p>但是,<code>Promise.all</code> <strong>当且仅当</strong>传入的可迭代对象为空时为同步:</p>
|
||||
<pre class="brush: js">var p = Promise.all([]); // will be immediately resolved
|
||||
<pre><code class="language-javascript">var p = Promise.all([]); // will be immediately resolved
|
||||
var p2 = Promise.all([1337, "hi"]); // non-promise values will be ignored, but the evaluation will be done asynchronously
|
||||
console.log(p);
|
||||
console.log(p2)
|
||||
@@ -105,10 +105,10 @@ setTimeout(function(){
|
||||
// Promise { <state>: "pending" }
|
||||
// the stack is now empty
|
||||
// Promise { <state>: "fulfilled", <value>: Array[2] }
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="Promise.all_的快速返回失败行为"><code>Promise.all</code> 的快速返回失败行为</h3>
|
||||
<p><code>Promise.all</code> 在任意一个传入的 <code>promise</code> 失败时返回失败。例如,如果你传入的 <code>promise</code>中,有四个 <code>promise</code> 在一定的时间之后调用成功函数,有一个立即调用失败函数,那么 <code>Promise.all</code> 将立即变为失败。</p>
|
||||
<pre class="brush: js">var p1 = new Promise((resolve, reject) => {
|
||||
<pre><code class="language-javascript">var p1 = new Promise((resolve, reject) => {
|
||||
setTimeout(resolve, 1000, 'one');
|
||||
});
|
||||
var p2 = new Promise((resolve, reject) => {
|
||||
@@ -143,7 +143,7 @@ Promise.all([p1, p2, p3, p4, p5]).then(values => {
|
||||
//From console:
|
||||
//"reject"
|
||||
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h2 id="技术规范">技术规范</h2>
|
||||
<table class="standard-table">
|
||||
<tbody>
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
p.catch(function(reason) {
|
||||
// 拒绝
|
||||
});
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="参数" style="line-height: 24px;">参数</h3>
|
||||
<dl>
|
||||
<dt><strong>onRejected</strong></dt>
|
||||
@@ -24,7 +24,7 @@ p.catch(function(reason) {
|
||||
<p>Internally calls <code>Promise.prototype.then</code> on the object upon which is called, passing the parameters <code>undefined</code> and the <code>onRejected</code> handler received; then returns the value of that call (which is a <a href="Reference/Global_Objects/Promise" title="Promise 对象用于表示一个异步操作的最终状态(完成或失败),以及该异步操作的结果值。"><code>Promise</code></a>).</p>
|
||||
<h2 id="示例" style="line-height: 30px;">示例</h2>
|
||||
<h3 id="使用链式语句的_catch方法" style="line-height: 24px;">使用链式语句的 <code>catch</code>方法</h3>
|
||||
<pre class="brush: js"><code>var p1 = new Promise(function(resolve, reject) {
|
||||
<pre><code class="language-javascript"><code>var p1 = new Promise(function(resolve, reject) {
|
||||
resolve('Success');
|
||||
});</code>
|
||||
|
||||
@@ -49,9 +49,9 @@ p1.then(function(value) {
|
||||
console.log('after a catch the chain is restored');
|
||||
}, function () {
|
||||
console.log('Not fired due to the catch');
|
||||
});</pre>
|
||||
});</code></pre>
|
||||
<h3 id="捕获抛出的错误" style="line-height: 30px;">捕获抛出的错误</h3>
|
||||
<pre class="brush: js"><code>// 抛出一个错误,大多数时候将调用catch方法
|
||||
<pre><code class="language-javascript"><code>// 抛出一个错误,大多数时候将调用catch方法
|
||||
var p1 = new Promise(function(resolve, reject) {
|
||||
throw 'Uh-oh!';
|
||||
});
|
||||
@@ -79,7 +79,7 @@ var p3 = new Promise(function(resolve, reject) {
|
||||
|
||||
p3.catch(function(e) {
|
||||
console.log(e); // 不会执行
|
||||
});</code></pre>
|
||||
});</code></code></pre>
|
||||
<h3 id="如果已决议" style="line-height: 30px;">如果已决议</h3>
|
||||
<pre><code>//创建一个新的 Promise ,且已决议
|
||||
var p1 = Promise.resolve("calling next");
|
||||
@@ -96,7 +96,7 @@ p2.then(function (value) {
|
||||
}, function (reason) {
|
||||
console.log("next promise's onRejected");
|
||||
console.log(reason);
|
||||
});</code></pre>
|
||||
});</code></code></pre>
|
||||
<h2 id="规范" style="line-height: 30px;">规范</h2>
|
||||
<table class="standard-table">
|
||||
<tbody>
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
<div> </div>
|
||||
<div><code><strong>finally()</strong></code> 方法返回一个<a href="Reference/Global_Objects/Promise" title="Promise 对象用于表示一个异步操作的最终状态(完成或失败),以及该异步操作的结果值。"><code>Promise</code></a>,在promise执行结束时,无论结果是fulfilled或者是rejected,在执行<a href="Reference/Global_Objects/Promise/then" title="then() 方法返回一个 Promise 。它最多需要有两个参数:Promise 的成功和失败情况的回调函数。"><code>then()</code></a>和<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>catch()</code></a>后,都会执行<strong><code>finally</code></strong>指定的回调函数。这为指定执行完promise后,无论结果是fulfilled还是rejected都需要执行的代码提供了一种方式,避免同样的语句需要在<a href="Reference/Global_Objects/Promise/then" title="then() 方法返回一个 Promise 。它最多需要有两个参数:Promise 的成功和失败情况的回调函数。"><code>then()</code></a>和<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>catch()</code></a>中各写一次的情况。</div>
|
||||
<h2 id="语法">语法</h2>
|
||||
<pre class="syntaxbox"><var>p.finally(onFinally)</var>;
|
||||
<pre><code class="language-javascript"><var>p.finally(onFinally)</var>;
|
||||
|
||||
p.finally(function() {
|
||||
// 返回状态为(resolved 或 rejected)
|
||||
});参数</pre>
|
||||
});参数</code></pre>
|
||||
<dl>
|
||||
<dt><code>onFinally</code></dt>
|
||||
<dd><code>Promise</code> 状态改变后执行的回调函数。</dd>
|
||||
@@ -27,7 +27,7 @@ p.finally(function() {
|
||||
<p><strong>注意:</strong> 在<code>finally</code>回调中 <code>throw</code>(或返回被拒绝的promise)将以 <code>throw()</code> 指定的原因拒绝新的promise.</p>
|
||||
</div>
|
||||
<h2 id="示例">示例</h2>
|
||||
<pre class="brush: js">let isLoading = true;
|
||||
<pre><code class="language-javascript">let isLoading = true;
|
||||
|
||||
fetch(myRequest).then(function(response) {
|
||||
var contentType = response.headers.get("content-type");
|
||||
@@ -40,7 +40,7 @@ fetch(myRequest).then(function(response) {
|
||||
.catch(function(error) { console.log(error); })
|
||||
.finally(function() { isLoading = false; });
|
||||
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h2 id="规范">规范</h2>
|
||||
<table class="standard-table">
|
||||
<tbody>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<div><iframe class="interactive interactive-js" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/promise-race.html" width="100%"></iframe></div>
|
||||
<p class="hidden">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 <a class="external" href="https://github.com/mdn/interactive-examples" rel="noopener">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p>
|
||||
<h2 id="Syntax" name="Syntax">语法</h2>
|
||||
<pre class="syntaxbox"><var>Promise.race(iterable)</var>;</pre>
|
||||
<pre><code class="language-javascript"><var>Promise.race(iterable)</var>;</code></pre>
|
||||
<h3 id="参数"><strong>参数</strong></h3>
|
||||
<dl>
|
||||
<dt>iterable</dt>
|
||||
@@ -35,9 +35,9 @@ setTimeout(function(){
|
||||
// logs, in order:
|
||||
// Promise { <state>: "pending" }
|
||||
// the stack is now empty
|
||||
// Promise { <state>: "fulfilled", <value>: 33 }</code></pre>
|
||||
// Promise { <state>: "fulfilled", <value>: 33 }</code></code></pre>
|
||||
<h3 id="使用_Promise.race_–_setTimeout_的示例"><code><font face="Open Sans, sans-serif">使用 </font>Promise.race</code> – <code>setTimeout 的示例</code></h3>
|
||||
<pre class="brush: js">var p1 = new Promise(function(resolve, reject) {
|
||||
<pre><code class="language-javascript">var p1 = new Promise(function(resolve, reject) {
|
||||
setTimeout(resolve, 500, "one");
|
||||
});
|
||||
var p2 = new Promise(function(resolve, reject) {
|
||||
@@ -76,7 +76,7 @@ Promise.race([p5, p6]).then(function(value) {
|
||||
console.log(reason); // "six"
|
||||
// p6 更快,所以它失败了
|
||||
});
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h2 id="规范">规范</h2>
|
||||
<table class="standard-table">
|
||||
<tbody>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<h2 id="Summary" name="Summary">概述</h2>
|
||||
<p><code><strong>Promise.reject(reason)</strong></code>方法返回一个带有拒绝原因reason参数的Promise对象。</p>
|
||||
<h2 id="Syntax" name="Syntax">语法</h2>
|
||||
<pre class="syntaxbox"><var>Promise.reject(reason)</var>;</pre>
|
||||
<pre><code class="language-javascript"><var>Promise.reject(reason)</var>;</code></pre>
|
||||
<h3 id="Parameters">Parameters</h3>
|
||||
<dl>
|
||||
<dt>reason</dt>
|
||||
@@ -17,7 +17,7 @@
|
||||
<p><font face="Open Sans, sans-serif">静态函数</font><code>Promise.reject</code>返回一个被拒绝的<code>Promise对象</code>。通过使用<a href="Reference/Global_Objects/Error" title="通过Error的构造器可以创建一个错误对象。当运行时错误产生时,Error的实例对象会被抛出。Error对象也可用于用户自定义的异常的基础对象。下面列出了各种内建的标准错误类型。"><code>Error</code></a>的实例获取错误原因<code>reason</code>对调试和选择性错误捕捉很有帮助。</p>
|
||||
<h2 id="示例">示例</h2>
|
||||
<h3 id="使用静态Promise.reject()方法">使用静态<code>Promise.reject()</code>方法</h3>
|
||||
<pre class="brush: js">Promise.reject("Testing static reject").then(function(reason) {
|
||||
<pre><code class="language-javascript">Promise.reject("Testing static reject").then(function(reason) {
|
||||
// 未被调用
|
||||
}, function(reason) {
|
||||
console.log(reason); // "Testing static reject"
|
||||
@@ -27,7 +27,7 @@ Promise.reject(new Error("fail")).then(function(result) {
|
||||
// 未被调用
|
||||
}, function(error) {
|
||||
console.log(error); // stacktrace
|
||||
});</pre>
|
||||
});</code></pre>
|
||||
<h2 id="规范">规范</h2>
|
||||
<table class="standard-table">
|
||||
<tbody>
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
<div><iframe class="interactive interactive-js" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/promise-resolve.html" width="100%"></iframe></div>
|
||||
<p class="hidden">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 <a class="external" href="https://github.com/mdn/interactive-examples" rel="noopener">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p>
|
||||
<h2 id="Syntax" name="Syntax">语法</h2>
|
||||
<pre class="syntaxbox"><var>Promise.resolve(value)</var>;
|
||||
<pre><code class="language-javascript"><var>Promise.resolve(value)</var>;
|
||||
Promise.resolve(promise);
|
||||
Promise.resolve(thenable);</pre>
|
||||
Promise.resolve(thenable);</code></pre>
|
||||
<h3 id="参数">参数</h3>
|
||||
<dl>
|
||||
<dt>value</dt>
|
||||
@@ -21,20 +21,20 @@ Promise.resolve(thenable);</pre>
|
||||
<p>静态方法 <code>Promise.resolve</code>返回一个解析过的<code>Promise</code>对象.</p>
|
||||
<h2 id="示例">示例</h2>
|
||||
<h3 id="使用静态方法Promise.resolve">使用静态方法<code>Promise.resolve</code></h3>
|
||||
<pre class="brush: js">Promise.resolve("Success").then(function(value) {
|
||||
<pre><code class="language-javascript">Promise.resolve("Success").then(function(value) {
|
||||
console.log(value); // "Success"
|
||||
}, function(value) {
|
||||
// 不会被调用
|
||||
});
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="对一个数组进行resolve">对一个数组进行resolve</h3>
|
||||
<pre class="brush: js">var p = Promise.resolve([1,2,3]);
|
||||
<pre><code class="language-javascript">var p = Promise.resolve([1,2,3]);
|
||||
p.then(function(v) {
|
||||
console.log(v[0]); // 1
|
||||
});
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="Resolve另一个promise对象">Resolve另一个promise对象</h3>
|
||||
<pre class="brush: js">var original = Promise.resolve('我在第二行');
|
||||
<pre><code class="language-javascript">var original = Promise.resolve('我在第二行');
|
||||
var cast = Promise.resolve(original);
|
||||
cast.then(function(value) {
|
||||
console.log('value: ' + value);
|
||||
@@ -45,9 +45,9 @@ console.log('original === cast ? ' + (original === cast));
|
||||
* 打印顺序如下,这里有一个同步异步先后执行的区别
|
||||
* original === cast ? true
|
||||
* value: 我在第二行
|
||||
*/</pre>
|
||||
*/</code></pre>
|
||||
<h3 id="resolve_thenable的对象们并抛出错误">resolve thenable的对象们并抛出错误</h3>
|
||||
<pre class="brush: js">// Resolve一个thenable对象
|
||||
<pre><code class="language-javascript">// Resolve一个thenable对象
|
||||
var p1 = Promise.resolve({
|
||||
then: function(onFulfill, onReject) { onFulfill("fulfilled!"); }
|
||||
});
|
||||
@@ -86,7 +86,7 @@ p3.then(function(v) {
|
||||
}, function(e) {
|
||||
// 不会被调用
|
||||
});
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h2 id="规范">规范</h2>
|
||||
<table class="standard-table">
|
||||
<tbody>
|
||||
|
||||
@@ -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