aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/promise/catch/index.html
blob: 3bd0a41fd4f0f93d6248e39db1583d885d846ced (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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>