diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-11 18:59:39 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-11 18:59:39 -0500 |
commit | d192fb918b0e2aa8869de6dcc59de8464b6e879a (patch) | |
tree | 3dc6f395a53be89041c52e9326baf908ffa3f3a6 /files/zh-cn/web/javascript/reference/global_objects/promise/all | |
parent | e3e12548adeb7e1dcfc4d5b32884a225ee2b499d (diff) | |
download | translated-content-d192fb918b0e2aa8869de6dcc59de8464b6e879a.tar.gz translated-content-d192fb918b0e2aa8869de6dcc59de8464b6e879a.tar.bz2 translated-content-d192fb918b0e2aa8869de6dcc59de8464b6e879a.zip |
dump 2020-12-11
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/promise/all')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/promise/all/index.html | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/promise/all/index.html b/files/zh-cn/web/javascript/reference/global_objects/promise/all/index.html index 84264ce68b..e81f816fbf 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/promise/all/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/promise/all/index.html @@ -13,9 +13,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Promise/all --- <div>{{JSRef}}</div> -<p><code><strong>Promise.all(iterable)</strong></code> 方法返回一个 {{jsxref("Promise")}} 实例,此实例在 <code>iterable</code> 参数内所有的 <code>promise</code> 都“完成(resolved)”或参数中不包含 <code>promise</code> 时回调完成(resolve);如果参数中 <code>promise</code> 有一个失败(rejected),此实例回调失败(reject),失败的原因是第一个失败 <code>promise</code> 的结果。</p> - -<p>它通常在启动多个异步任务并发运行并为其结果创建承诺之后使用,以便人们可以等待所有任务完成。</p> +<p>Promise.all() 方法接收一个promise的idterable类型(注:Array,Map,Set都属于ES6的iterable类型)的输入,并且只返回一个{{jsxref("Promise")}}实例, 那个输入的所有promise的resolve回调的结果是一个数组。这个{{jsxref("Promise")}}的resolve回调执行是在所有输入的promise的resolve回调都结束,或者输入的iterable里没有promise了的时候。它的reject回调执行是,只要任何一个输入的promise的reject回调执行或者输入不合法的promise就会立即抛出错误,并且reject的是第一个抛出的错误信息。</p> <div>{{EmbedInteractiveExample("pages/js/promise-all.html")}}</div> @@ -23,7 +21,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Promise/all <h2 id="语法">语法</h2> -<pre class="syntaxbox"><var>Promise.all(iterable)</var>;</pre> +<pre class="syntaxbox notranslate"><var>Promise.all(iterable)</var>;</pre> <h3 id="参数">参数</h3> @@ -58,7 +56,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Promise/all <p><code>Promise.all</code> 等待所有都完成(或第一个失败)。</p> -<pre class="brush: js">var p1 = Promise.resolve(3); +<pre class="brush: js notranslate">var p1 = Promise.resolve(3); var p2 = 1337; var p3 = new Promise((resolve, reject) => { setTimeout(resolve, 100, 'foo'); @@ -70,7 +68,7 @@ Promise.all([p1, p2, p3]).then(values => { <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 class="brush: js notranslate">// 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)]); @@ -93,7 +91,7 @@ setTimeout(function(){ <p>下面的例子中演示了 <code>Promise.all</code> 的异步性(如果传入的可迭代对象是空的,就是同步):</p> -<pre class="brush: js">// we are passing as argument an array of promises that are already resolved, +<pre class="brush: js notranslate">// 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)]; @@ -115,7 +113,7 @@ setTimeout(function(){ <p>如果 <code>Promise.all</code> 失败,也是一样的:</p> -<pre class="brush: js">var mixedPromisesArray = [Promise.resolve(33), Promise.reject(44)]; +<pre class="brush: js notranslate">var mixedPromisesArray = [Promise.resolve(33), Promise.reject(44)]; var p = Promise.all(mixedPromisesArray); console.log(p); setTimeout(function(){ @@ -131,7 +129,7 @@ setTimeout(function(){ <p>但是,<code>Promise.all</code> <strong>当且仅当</strong>传入的可迭代对象为空时为同步:</p> -<pre class="brush: js">var p = Promise.all([]); // will be immediately resolved +<pre class="brush: js notranslate">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) @@ -151,7 +149,7 @@ setTimeout(function(){ <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 class="brush: js notranslate">var p1 = new Promise((resolve, reject) => { setTimeout(resolve, 1000, 'one'); }); var p2 = new Promise((resolve, reject) => { |