aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/promise/resolve
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/resolve
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/resolve')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/promise/resolve/index.html158
1 files changed, 158 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/promise/resolve/index.html b/files/zh-cn/web/javascript/reference/global_objects/promise/resolve/index.html
new file mode 100644
index 0000000000..607fa09f36
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/promise/resolve/index.html
@@ -0,0 +1,158 @@
+---
+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>{{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 class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
+
+<p>{{Compat("javascript.builtins.Promise.resolve")}}</p>
+
+<h2 id="参见">参见</h2>
+
+<ul>
+ <li>{{jsxref("Promise")}}</li>
+</ul>