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
157
158
159
160
161
162
163
164
165
166
|
---
title: Promise.resolve()
slug: Web/JavaScript/Reference/Global_Objects/Promise/resolve
tags:
- ECMAScript 2015
- JavaScript
- Promise
translation_of: Web/JavaScript/Reference/Global_Objects/Promise/resolve
---
<div>{{JSRef}}</div>
<div><code><strong>Promise.resolve(value)</strong></code>方法返回一个以给定值解析后的{{jsxref("Promise")}} 对象。如果这个值是一个 promise ,那么将返回这个 promise ;如果这个值是thenable(即带有{{jsxref("Promise.then", "\"then\" ")}}方法),返回的promise会“跟随”这个thenable的对象,采用它的最终状态;否则返回的promise将以此值完成。<span style="font-size: 1rem; letter-spacing: -0.00278rem;">此函数将类promise对象的多层嵌套展平。</span></div>
<div></div>
<div></div>
<div class="blockIndicator warning">
<p>警告:不要在解析为自身的thenable 上调用<code>Promise.resolve</code>。这将导致无限递归,因为它试图展平无限嵌套的promise。一个例子是将它与Angular中的异步管道一起使用。在<a href="https://angular.io/guide/template-syntax#avoid-side-effects">此处</a>了解更多信息。</p>
</div>
<div>例如下例代码</div>
<pre class="brush: js">
let thenable = {
then: (resolve, reject) => {
resolve(thenable)
}
}
Promise.resolve(thenable) //这会造成一个死循环
</pre>
<div>{{EmbedInteractiveExample("pages/js/promise-resolve.html")}}</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 href="https://github.com/mdn/interactive-examples">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>
<h3 id="参数">参数</h3>
<dl>
<dt>value</dt>
</dl>
<p>将被<code>Promise</code>对象解析的参数,也可以是一个<code>Promise</code>对象,或者是一个thenable。</p>
<h3 id="返回值">返回值</h3>
<p>返回一个带着给定值解析过的<code>Promise</code>对象,如果参数本身就是一个<code>Promise</code>对象,则直接返回这个<code>Promise</code>对象。</p>
<h2 id="Description" name="Description">描述</h2>
<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) {
console.log(value); // "Success"
}, function(value) {
// 不会被调用
});
</pre>
<h3 id="resolve一个数组">resolve一个数组</h3>
<pre class="brush: js">var p = Promise.resolve([1,2,3]);
p.then(function(v) {
console.log(v[0]); // 1
});
</pre>
<h3 id="resolve另一个promise">resolve另一个promise</h3>
<pre class="brush: js">var original = Promise.resolve(33);
var cast = Promise.resolve(original);
cast.then(function(value) {
console.log('value: ' + value);
});
console.log('original === cast ? ' + (original === cast));
/*
* 打印顺序如下,这里有一个同步异步先后执行的区别
* original === cast ? true
* value: 33
*/</pre>
<p>日志顺序颠倒其实是由于异步地调用<code>then</code> 方法。在<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#Return_value">这里</a>查看<code>then</code> 是如何工作的。</p>
<h3 id="resolve_thenable_并抛出错误">resolve thenable 并抛出错误</h3>
<pre class="brush: js">// Resolve一个thenable对象
var p1 = Promise.resolve({
then: function(onFulfill, onReject) { onFulfill("fulfilled!"); }
});
console.log(p1 instanceof Promise) // true, 这是一个Promise对象
p1.then(function(v) {
console.log(v); // 输出"fulfilled!"
}, function(e) {
// 不会被调用
});
// Thenable在callback之前抛出异常
// Promise rejects
var thenable = { then: function(resolve) {
throw new TypeError("Throwing");
resolve("Resolving");
}};
var p2 = Promise.resolve(thenable);
p2.then(function(v) {
// 不会被调用
}, function(e) {
console.log(e); // TypeError: Throwing
});
// Thenable在callback之后抛出异常
// Promise resolves
var thenable = { then: function(resolve) {
resolve("Resolving");
throw new TypeError("Throwing");
}};
var p3 = Promise.resolve(thenable);
p3.then(function(v) {
console.log(v); // 输出"Resolving"
}, function(e) {
// 不会被调用
});
</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-promise.resolve', 'Promise.resolve')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>ECMA标准中的首次定义</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-promise.resolve', 'Promise.resolve')}}</td>
<td>{{Spec2('ES6')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("javascript.builtins.Promise.resolve")}}</p>
<h2 id="参见">参见</h2>
<ul>
<li>{{jsxref("Promise")}}</li>
</ul>
|