aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/promise/catch/index.html
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/promise/catch/index.html
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/promise/catch/index.html')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/promise/catch/index.html156
1 files changed, 156 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/promise/catch/index.html b/files/zh-cn/web/javascript/reference/global_objects/promise/catch/index.html
new file mode 100644
index 0000000000..3bd0a41fd4
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/promise/catch/index.html
@@ -0,0 +1,156 @@
+---
+title: Promise.prototype.catch()
+slug: Web/JavaScript/Reference/Global_Objects/Promise/catch
+tags:
+ - Promise
+ - Promise.prototype.catch()
+translation_of: Web/JavaScript/Reference/Global_Objects/Promise/catch
+---
+<div>{{JSRef}}</div>
+
+<p><strong>catch()</strong> 方法返回一个<a href="/zh-CN/docs/Web/API/Promise">Promise</a>,并且处理拒绝的情况。它的行为与调用{{jsxref("Promise.then", "Promise.prototype.then(undefined, onRejected)")}} 相同。 (事实上, calling <code>obj.catch(onRejected)</code> 内部calls <code>obj.then(undefined, onRejected)</code>).</p>
+
+<h2 id="Syntax" name="Syntax" style="line-height: 30px;">语法</h2>
+
+<pre class="syntaxbox" style="font-size: 14px;"><var>p.catch(onRejected)</var>;
+
+p.catch(function(reason) {
+ // 拒绝
+});
+</pre>
+
+<h3 id="参数" style="line-height: 24px;">参数</h3>
+
+<dl>
+ <dt><strong>onRejected</strong></dt>
+ <dd>当Promise 被rejected时,被调用的一个{{jsxref("Function")}}。 该函数拥有一个参数:</dd>
+ <dd><code>reason</code>    rejection 的原因。</dd>
+ <dd>
+ <p> 如果 <code>onRejected</code> 抛出一个错误或返回一个本身失败的 Promise ,  通过 <code>catch()</code> 返回的Promise 被rejected;否则,它将显示为成功(resolved)。 </p>
+
+ <h3 id="返回值">返回值</h3>
+
+ <p>一个{{jsxref("Promise")}}.</p>
+ </dd>
+</dl>
+
+<h2 id="Description" name="Description" style="line-height: 30px;">描述</h2>
+
+<p><code>catch </code>方法可以用于您的promise组合中的错误处理。</p>
+
+<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 {{jsxref("Promise")}}).</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) {
+ resolve('Success');
+});</code>
+
+p1.then(function(value) {
+ console.log(value); // "Success!"
+ throw 'oh, no!';
+}).catch(function(e) {
+ console.log(e); // "oh, no!"
+}).then(function(){
+ console.log('after a catch the chain is restored');
+}, function () {
+ console.log('Not fired due to the catch');
+});
+
+// 以下行为与上述相同
+p1.then(function(value) {
+ console.log(value); // "Success!"
+ return Promise.reject('oh, no!');
+}).catch(function(e) {
+ console.log(e); // "oh, no!"
+}).then(function(){
+ console.log('after a catch the chain is restored');
+}, function () {
+ console.log('Not fired due to the catch');
+});</pre>
+
+<h3 id="捕获抛出的错误" style="line-height: 30px;">捕获抛出的错误</h3>
+
+<pre class="brush: js"><code>// 抛出一个错误,大多数时候将调用catch方法
+var p1 = new Promise(function(resolve, reject) {
+ throw 'Uh-oh!';
+});
+
+p1.catch(function(e) {
+ console.log(e); // "Uh-oh!"
+});
+
+// 在异步函数中抛出的错误不会被catch捕获到
+var p2 = new Promise(function(resolve, reject) {
+ setTimeout(function() {
+ throw 'Uncaught Exception!';
+ }, 1000);
+});
+
+p2.catch(function(e) {
+ console.log(e); // 不会执行
+});
+
+// 在resolve()后面抛出的错误会被忽略
+var p3 = new Promise(function(resolve, reject) {
+ resolve();
+ throw 'Silenced Exception!';
+});
+
+p3.catch(function(e) {
+ console.log(e); // 不会执行
+});</code></pre>
+
+<h3 id="如果已决议" style="line-height: 30px;">如果已决议</h3>
+
+<pre><code>//创建一个新的 Promise ,且已决议
+var p1 = Promise.resolve("calling next");
+
+var p2 = p1.catch(function (reason) {
+ //这个方法永远不会调用
+ console.log("catch p1!");
+ console.log(reason);
+});
+
+p2.then(function (value) {
+ console.log("next promise's onFulfilled"); /* next promise's onFulfilled */
+ console.log(value); /* calling next */
+}, function (reason) {
+ console.log("next promise's onRejected");
+ console.log(reason);
+});</code></pre>
+
+<h2 id="规范" style="line-height: 30px;">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">规范</th>
+ <th scope="col">状态</th>
+ <th scope="col">备注</th>
+ </tr>
+ <tr>
+ <td><a href="https://github.com/domenic/promises-unwrapping">domenic/promises-unwrapping</a></td>
+ <td>{{Spec2('ES6')}}</td>
+ <td>Initial definition in an ECMA standard.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-promise.prototype.catch', 'Promise.prototype.catch')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性" style="line-height: 30px;">浏览器兼容性</h2>
+
+<p>{{Compat("javascript/promise","Promise.prototype.catch")}}</p>
+
+<h2 id="相关链接" style="line-height: 30px;">相关链接</h2>
+
+<ul>
+ <li>{{jsxref("Promise")}}</li>
+ <li>{{jsxref("Promise.prototype.then()")}}</li>
+</ul>