aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/javascript/reference/global_objects/promise
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:43:23 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:43:23 -0500
commit218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (patch)
treea9ef8ac1e1b8fe4207b6d64d3841bfb8990b6fd0 /files/zh-tw/web/javascript/reference/global_objects/promise
parent074785cea106179cb3305637055ab0a009ca74f2 (diff)
downloadtranslated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.gz
translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.bz2
translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.zip
initial commit
Diffstat (limited to 'files/zh-tw/web/javascript/reference/global_objects/promise')
-rw-r--r--files/zh-tw/web/javascript/reference/global_objects/promise/all/index.html207
-rw-r--r--files/zh-tw/web/javascript/reference/global_objects/promise/catch/index.html189
-rw-r--r--files/zh-tw/web/javascript/reference/global_objects/promise/finally/index.html102
-rw-r--r--files/zh-tw/web/javascript/reference/global_objects/promise/index.html256
-rw-r--r--files/zh-tw/web/javascript/reference/global_objects/promise/prototype/index.html64
-rw-r--r--files/zh-tw/web/javascript/reference/global_objects/promise/race/index.html171
-rw-r--r--files/zh-tw/web/javascript/reference/global_objects/promise/reject/index.html72
-rw-r--r--files/zh-tw/web/javascript/reference/global_objects/promise/resolve/index.html142
-rw-r--r--files/zh-tw/web/javascript/reference/global_objects/promise/then/index.html271
9 files changed, 1474 insertions, 0 deletions
diff --git a/files/zh-tw/web/javascript/reference/global_objects/promise/all/index.html b/files/zh-tw/web/javascript/reference/global_objects/promise/all/index.html
new file mode 100644
index 0000000000..eb18ce63f9
--- /dev/null
+++ b/files/zh-tw/web/javascript/reference/global_objects/promise/all/index.html
@@ -0,0 +1,207 @@
+---
+title: Promise.all()
+slug: Web/JavaScript/Reference/Global_Objects/Promise/all
+translation_of: Web/JavaScript/Reference/Global_Objects/Promise/all
+---
+<div>{{JSRef}}</div>
+
+<p><code><strong>Promise.all()</strong></code> 方法回傳一個 {{jsxref("Promise")}} 物件,當引數 <code>iterable</code> 中所有的 promises 都被實現(resolved),或引數 iterable 不含任何 promise 時,被實現。或以第一個被拒絕的 promise 的原因被拒絕。</p>
+
+<h2 id="語法">語法</h2>
+
+<pre class="syntaxbox"><var>Promise.all(iterable)</var>;</pre>
+
+<dl>
+ <dt>iterable</dt>
+ <dd>一個 <a href="https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol">iterable</a> 物件像是 {{jsxref("Array")}} 或 {{jsxref("String")}}。</dd>
+</dl>
+
+<h3 id="回傳值">回傳值</h3>
+
+<ul>
+ <li>一個<strong>已被實現(already resolved)</strong>的 {{jsxref("Promise")}},若傳入的 iterable 為空。</li>
+ <li>一個<strong>非同步地被實現(asynchronously resolved)</strong>的 {{jsxref("Promise")}} 若傳入的 iterable 不含 promise。注意,Google Chrome 58 對此情形回傳一個<strong>已被解決</strong>的 promise。</li>
+ <li>一個<strong>擱置(pending)</strong>的 {{jsxref("Promise")}},對所有剩餘情形。此 promise 接著被<strong>非同步地</strong>被 resolved/rejected(只要堆疊為空)當 iterable 中所有的 promises 都被實現,或其中一個被拒絕。參見下方關於"Promise.all 的非同步與同步性質"的例子。</li>
+</ul>
+
+<h2 id="描述">描述</h2>
+
+<p>此方法在聚集(aggregating)多個 promises 的結果時很有幫助。</p>
+
+<p>實現(Fulfillment):<br>
+ 若傳入空的 iterable,此方法(同步地)回傳一個已被解決的 promise。若所有傳入的 promises 都被實現,或都不是 promise,<code>Promise.all</code> 回傳的 promise 被非同步地實現。無論是哪個情形,回傳一個以 iterable 其內<strong>所有</strong>值(包含非 promise 值)作為引數的陣列被實現。<br>
+  </p>
+
+<p>拒絕(Rejection):<br>
+ 若任一個傳入的 promise 被拒絕,Promise.all 非同步地以其值被拒絕,無論其他 promises 是否被解決。</p>
+
+<h2 id="範例">範例</h2>
+
+<h3 id="使用_Promise.all">使用 <code>Promise.all</code></h3>
+
+<p><code>Promise.all</code> 等到全部實現(或一個拒絕)。</p>
+
+<pre class="brush: js">var p1 = Promise.resolve(3);
+var p2 = 1337;
+var p3 = new Promise((resolve, reject) =&gt; {
+ setTimeout(resolve, 100, 'foo');
+});
+
+Promise.all([p1, p2, p3]).then(values =&gt; {
+ console.log(values); // [3, 1337, "foo"]
+});</pre>
+
+<p>若 iterable 含非 promise 值,它們將被忽略,但依然會被記入回傳 promise 陣列值(若被實現):</p>
+
+<pre class="brush: js">// 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)]);
+// this will be counted as if the iterable passed contains only the rejected promise with value "555", so it gets rejected
+var p3 = Promise.all([1,2,3, Promise.reject(555)]);
+
+// using setTimeout we can execute code after the stack is empty
+setTimeout(function(){
+ console.log(p);
+ console.log(p2);
+ console.log(p3);
+});
+
+// logs
+// Promise { &lt;state&gt;: "fulfilled", &lt;value&gt;: Array[3] }
+// Promise { &lt;state&gt;: "fulfilled", &lt;value&gt;: Array[4] }
+// Promise { &lt;state&gt;: "rejected", &lt;reason&gt;: 555 }</pre>
+
+<h3 id="Promise.all_的非同步與同步性質"><code>Promise.all</code> 的非同步與同步性質</h3>
+
+<p>以下例子驗證了 <code>Promise.all</code> 的非同步性質(asynchronicity)(或同步性質(synchronicity),若傳入的 iterable 是空的):</p>
+
+<pre class="brush: js">// 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)];
+
+var p = Promise.all(resolvedPromisesArray);
+// immediately logging the value of p
+console.log(p);
+
+// using setTimeout we can execute code after the stack is empty
+setTimeout(function(){
+ console.log('the stack is now empty');
+ console.log(p);
+});
+
+// logs, in order:
+// Promise { &lt;state&gt;: "pending" }
+// the stack is now empty
+// Promise { &lt;state&gt;: "fulfilled", &lt;value&gt;: Array[2] }
+</pre>
+
+<p><code>當</code> <code>Promise.all</code> 被拒絕時發生一樣的事情:</p>
+
+<pre class="brush: js">var mixedPromisesArray = [Promise.resolve(33), Promise.reject(44)];
+var p = Promise.all(mixedPromisesArray);
+console.log(p);
+setTimeout(function(){
+ console.log('the stack is now empty');
+ console.log(p);
+});
+
+// logs
+// Promise { &lt;state&gt;: "pending" }
+// the stack is now empty
+// Promise { &lt;state&gt;: "rejected", &lt;reason&gt;: 44 }
+</pre>
+
+<p>注意!<code>Promise.all</code> 同步地被解決<strong>若且唯若</strong>傳入的 iterable 為空:</p>
+
+<pre class="brush: js">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)
+setTimeout(function(){
+ console.log('the stack is now empty');
+ console.log(p2);
+});
+
+// logs
+// Promise { &lt;state&gt;: "fulfilled", &lt;value&gt;: Array[0] }
+// Promise { &lt;state&gt;: "pending" }
+// the stack is now empty
+// Promise { &lt;state&gt;: "fulfilled", &lt;value&gt;: Array[2] }
+</pre>
+
+<h3 id="Promise.all_的失敗優先(fail-fast)行為"><code>Promise.all</code> 的失敗優先(fail-fast)行為</h3>
+
+<p><code>當任一個陣列成員被拒絕則</code> <code>Promise.all</code> 被拒絕。例如,若傳入四個將在一段時間後被解決的 promises,而其中一個立刻被拒絕,則 <code>Promise.all</code> 將立刻被拒絕。</p>
+
+<pre class="brush: js">var p1 = new Promise((resolve, reject) =&gt; {
+ setTimeout(resolve, 1000, 'one');
+});
+var p2 = new Promise((resolve, reject) =&gt; {
+ setTimeout(resolve, 2000, 'two');
+});
+var p3 = new Promise((resolve, reject) =&gt; {
+ setTimeout(resolve, 3000, 'three');
+});
+var p4 = new Promise((resolve, reject) =&gt; {
+ setTimeout(resolve, 4000, 'four');
+});
+var p5 = new Promise((resolve, reject) =&gt; {
+ reject('reject');
+});
+
+Promise.all([p1, p2, p3, p4, p5]).then(values =&gt; {
+ console.log(values);
+}, reason =&gt; {
+ console.log(reason)
+});
+
+//From console:
+//"reject"
+
+//You can also use .catch
+Promise.all([p1, p2, p3, p4, p5]).then(values =&gt; {
+ console.log(values);
+}).catch(reason =&gt; {
+ console.log(reason)
+});
+
+//From console:
+//"reject"
+
+</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('ES2015', '#sec-promise.all', 'Promise.all')}}</td>
+ <td>{{Spec2('ES2015')}}</td>
+ <td>Initial definition in an ECMA standard.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-promise.all', 'Promise.all')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
+
+<p class="hidden">To contribute to this compatibility data, please write a pull request against this repository: <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a>.</p>
+
+<p>{{Compat("javascript.builtins.Promise.all")}}</p>
+
+<h2 id="參見">參見</h2>
+
+<ul>
+ <li>{{jsxref("Promise")}}</li>
+ <li>{{jsxref("Promise.race()")}}</li>
+</ul>
diff --git a/files/zh-tw/web/javascript/reference/global_objects/promise/catch/index.html b/files/zh-tw/web/javascript/reference/global_objects/promise/catch/index.html
new file mode 100644
index 0000000000..d06036c11e
--- /dev/null
+++ b/files/zh-tw/web/javascript/reference/global_objects/promise/catch/index.html
@@ -0,0 +1,189 @@
+---
+title: Promise.prototype.catch()
+slug: Web/JavaScript/Reference/Global_Objects/Promise/catch
+tags:
+ - EMCAScript 2015
+ - JavaScript
+ - Method
+ - Promise
+ - Prototype
+translation_of: Web/JavaScript/Reference/Global_Objects/Promise/catch
+---
+<div>{{JSRef}}</div>
+
+<p><strong>catch()</strong> 方法只處理 Promise 的被拒絕狀態,並回傳一個新的 <code>Promise</code> 物件。此方法的行為等同於呼叫 {{jsxref("Promise.then", "Promise.prototype.then(undefined, onRejected)")}}。</p>
+
+<h2 id="語法">語法</h2>
+
+<pre class="syntaxbox"><var>p.catch(onRejected)</var>;
+
+p.catch(function(reason) {
+ // rejection
+});
+</pre>
+
+<h3 id="參數">參數</h3>
+
+<dl>
+ <dt>onRejected</dt>
+ <dd>一個 {{jsxref("Function")}} ,在 <code>Promise</code> 被拒絕時被呼叫。這個函式有一個引數:
+ <dl>
+ <dt><code>reason</code></dt>
+ <dd>失敗訊息。</dd>
+ </dl>
+ 若 onRejected 拋出一個錯誤或回傳一個被拒絕的 Promise,則 catch() 回傳的 Promise 被拒絕;其他情形都是被實現。</dd>
+</dl>
+
+<h3 id="回傳值">回傳值</h3>
+
+<p>呼叫(<code>catch</code> 的 promise)物件,內部呼叫 <code>Promise.prototype.then</code>,傳入引數 undefined 及 onRejected;接著以之結果回傳(結果為 {{jsxref("Promise")}})。</p>
+
+<p><strong>內部呼叫演示:</strong></p>
+
+<pre class="brush: js">// overriding original Promise.prototype.then/catch just to add some logs
+(function(Promise){
+    var originalThen = Promise.prototype.then;
+    var originalCatch = Promise.prototype.catch;
+
+    Promise.prototype.then = function(){
+        console.log('&gt; &gt; &gt; &gt; &gt; &gt; called .then on %o with arguments: %o', this, arguments);
+        return originalThen.apply(this, arguments);
+    };
+    Promise.prototype.catch = function(){
+        console.log('&gt; &gt; &gt; &gt; &gt; &gt; called .catch on %o with arguments: %o', this, arguments);
+        return originalCatch.apply(this, arguments);
+    };
+
+})(this.Promise);
+
+
+
+// calling catch on an already resolved promise
+Promise.resolve().catch(function XXX(){});
+
+// logs:
+// &gt; &gt; &gt; &gt; &gt; &gt; called .catch on Promise{} with arguments: Arguments{1} [0: function XXX()]
+// &gt; &gt; &gt; &gt; &gt; &gt; called .then on Promise{} with arguments: Arguments{2} [0: undefined, 1: function XXX()]
+</pre>
+
+<h2 id="描述">描述</h2>
+
+<p><code>catch</code> 方法在處理 promise 組合的錯誤時很有幫助。</p>
+
+<h2 id="範例">範例</h2>
+
+<h3 id="使用及串接_catch_方法">使用及串接 <code>catch</code> 方法</h3>
+
+<pre class="brush: js">var p1 = new Promise(function(resolve, reject) {
+  resolve('Success');
+});
+
+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');
+});
+
+// The following behaves the same as above
+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="拋出例外時的陷阱">拋出例外時的陷阱</h3>
+
+<pre class="brush: js">// Throwing an error will call the catch method most of the time
+var p1 = new Promise(function(resolve, reject) {
+  throw 'Uh-oh!';
+});
+
+p1.catch(function(e) {
+  console.log(e); // "Uh-oh!"
+});
+
+// Errors thrown inside asynchronous functions will act like uncaught errors
+var p2 = new Promise(function(resolve, reject) {
+  setTimeout(function() {
+  throw 'Uncaught Exception!';
+  }, 1000);
+});
+
+p2.catch(function(e) {
+  console.log(e); // This is never called
+});
+
+// Errors thrown after resolve is called will be silenced
+var p3 = new Promise(function(resolve, reject) {
+ resolve();
+  throw 'Silenced Exception!';
+});
+
+p3.catch(function(e) {
+   console.log(e); // This is never called
+});</pre>
+
+<h3 id="如果_Promise_被實現">如果 Promise 被實現</h3>
+
+<pre class="brush: js">//Create a promise which would not call onReject
+var p1 = Promise.resolve("calling next");
+
+var p2 = p1.catch(function (reason) {
+    //This is never called
+    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);
+});</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('ES2015', '#sec-promise.prototype.catch', 'Promise.prototype.catch')}}</td>
+ <td>{{Spec2('ES2015')}}</td>
+ <td>Initial definition in an ECMA standard.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-promise.prototype.catch', 'Promise.prototype.catch')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
+
+<p class="hidden">To contribute to this compatibility data, please write a pull request against this repository: <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a>.</p>
+
+<p>{{Compat("javascript.builtins.Promise.catch")}}</p>
+
+<h2 id="參見">參見</h2>
+
+<ul>
+ <li>{{jsxref("Promise")}}</li>
+ <li>{{jsxref("Promise.prototype.then()")}}</li>
+</ul>
diff --git a/files/zh-tw/web/javascript/reference/global_objects/promise/finally/index.html b/files/zh-tw/web/javascript/reference/global_objects/promise/finally/index.html
new file mode 100644
index 0000000000..eef15faf9a
--- /dev/null
+++ b/files/zh-tw/web/javascript/reference/global_objects/promise/finally/index.html
@@ -0,0 +1,102 @@
+---
+title: Promise.prototype.finally()
+slug: Web/JavaScript/Reference/Global_Objects/Promise/finally
+translation_of: Web/JavaScript/Reference/Global_Objects/Promise/finally
+---
+<div>{{JSRef}}</div>
+
+<p><code><strong>finally()</strong></code> 方法會回傳一個 {{jsxref("Promise")}}。當 promise 被 settled 後,無論其結果是 fulfilled 還是 rejected ,都會執行指定的回呼函數。它提供了一個讓 <code>Promise</code> 在被確認後,無論是 fulfilled 或是 rejected 都會執行某些程式碼的一種手段。</p>
+
+<p>這樣可以避免你在 promise 的 {{jsxref("Promise.then", "then()")}} 和 {{jsxref("Promise.catch", "catch()")}} 重複處理相同的程式碼。</p>
+
+<h2 id="Syntax">Syntax</h2>
+
+<pre class="syntaxbox"><var>p.finally(onFinally)</var>;
+
+p.finally(function() {
+ // settled(fulfilled 或 rejected)
+});
+</pre>
+
+<h3 id="Parameters">Parameters</h3>
+
+<dl>
+ <dt><code>onFinally</code></dt>
+ <dd>當 <code>Promise</code> settled 後呼叫的 {{jsxref("Function")}}。</dd>
+</dl>
+
+<h3 id="Return_value">Return value</h3>
+
+<p>回傳 {{jsxref("Promise")}} 當 <code>finally</code> 的處理函數 <code>onFinally</code> 被指定時。</p>
+
+<h2 id="Description">Description</h2>
+
+<p>當你希望在 promise settled 後且不關心它的結果為何時,執行一些處理或清理的工作, <code>finally()</code> 方法會很有幫助。</p>
+
+<p><code>finally()</code> 方法非常類似於 <code>.then(onFinally, onFinally)</code> 的呼叫方式,但仍有一些差異:</p>
+
+<ul>
+ <li><font><font>當建立行內的函數時,可以只傳遞一次,從而避免重複宣告或為它宣告變數。</font></font></li>
+ <li><code>finally</code> 的回呼函數並不會接收到任何引數,因其沒有可靠的方式來確認 promise 是被 fulfilled 還是 rejected 。它的使用情境僅適用於當你<em>不關心</em> rejection 的原因或 fulfillment 的值,因此無須提供。範例:
+ <ul>
+ <li><font><font>與 </font></font><code>Promise.resolve(2).then(() =&gt; {}, () =&gt; {})</code><font><font>(將被 resolved 為</font></font><code>undefined</code><font><font>)不同,</font></font><code>Promise.resolve(2).finally(() =&gt; {})</code><font><font> 將被 resolved 為</font></font><code>2</code><font><font>。</font></font></li>
+ <li><font><font>同樣的,與 </font></font><code>Promise.reject(3).then(() =&gt; {}, () =&gt; {})</code><font><font>(將 fulfilled 為</font></font><code>undefined</code><font><font>)不同,</font></font><code>Promise.reject(3).finally(() =&gt; {})</code><font><font> 將被 rejected 為</font></font><code>3</code><font><font>。</font></font></li>
+ </ul>
+ </li>
+</ul>
+
+<div class="note">
+<p><strong>備註: </strong>在 finally 回呼中使用 throw (或回傳 rejected promise)會導致新的 promise 被 reject , reject 的原因則是呼叫 throw() 時所指定的值。</p>
+</div>
+
+<h2 id="Examples">Examples</h2>
+
+<pre class="brush: js">let isLoading = true;
+
+fetch(myRequest).then(function(response) {
+ var contentType = response.headers.get("content-type");
+ if(contentType &amp;&amp; contentType.includes("application/json")) {
+ return response.json();
+ }
+ throw new TypeError("Oops, we haven't got JSON!");
+ })
+ .then(function(json) { /* process your JSON further */ })
+ .catch(function(error) { console.log(error); })
+ .finally(function() { isLoading = false; });
+
+</pre>
+
+<div class="hidden">
+<p>Please do not add polyfills on MDN pages. For more details, refer to: <a href="https://discourse.mozilla.org/t/mdn-rfc-001-mdn-wiki-pages-shouldnt-be-a-distributor-of-polyfills/24500">https://discourse.mozilla.org/t/mdn-rfc-001-mdn-wiki-pages-shouldnt-be-a-distributor-of-polyfills/24500</a></p>
+</div>
+
+<h2 id="Specifications">Specifications</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('ESDraft', '#sec-promise.prototype.finally', 'Promise.prototype.finally')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility">Browser compatibility</h2>
+
+<p class="hidden">To contribute to this compatibility data, please write a pull request against this repository: <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a>.</p>
+
+<p>{{Compat("javascript.builtins.Promise.finally")}}</p>
+
+<h2 id="See_also">See also</h2>
+
+<ul>
+ <li>{{jsxref("Promise")}}</li>
+ <li>{{jsxref("Promise.prototype.then()")}}</li>
+ <li>{{jsxref("Promise.prototype.catch()")}}</li>
+</ul>
diff --git a/files/zh-tw/web/javascript/reference/global_objects/promise/index.html b/files/zh-tw/web/javascript/reference/global_objects/promise/index.html
new file mode 100644
index 0000000000..8ec1456ae1
--- /dev/null
+++ b/files/zh-tw/web/javascript/reference/global_objects/promise/index.html
@@ -0,0 +1,256 @@
+---
+title: Promise
+slug: Web/JavaScript/Reference/Global_Objects/Promise
+translation_of: Web/JavaScript/Reference/Global_Objects/Promise
+---
+<div>{{JSRef}}</div>
+
+<p><strong><code>Promise</code></strong> 物件代表一個即將完成、或失敗的非同步操作,以及它所產生的值。</p>
+
+<div class="note">
+<p>此條目為介紹 Promise 建構式。要瞭解 Promise 相關使用方式,請先參考<a href="/zh-TW/docs/Web/JavaScript/Guide/Using_promises">使用 Promise</a>。Promise 建構式主要用於包裹尚未支援 Promise 的函式。</p>
+</div>
+
+<div>{{EmbedInteractiveExample("pages/js/promise-constructor.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="語法">語法</h2>
+
+<pre class="brush: js">new Promise( /* executor */ function(resolve, reject) { ... } );</pre>
+
+<h3 id="參數">參數</h3>
+
+<dl>
+ <dt>executor</dt>
+ <dd>為一個依序接收兩個參數的函式:<code>resolve</code> 及 <code>reject</code>(實現及拒絕回呼函式)。在 Promise 實作中,<code>executor</code> 函式在傳入參數 <code>resolve</code> 與 <code>reject</code> 後會立刻執行(<code>executor</code> 函式會在 <code>Promise</code> 建構式回傳 Promise 物件前被執行)。<code>resolve</code> 與 <code>reject</code> 函式,會在被個別呼叫時,個別執行之。通常 executor 函式會發起一些非同步操作。接著,成功完成後執行 <code>resolve</code> 以完成 promise;或如果有錯誤,執行 <code>rejects</code>。<br>如果 executor 函式在執行中拋出錯誤,promise 會被拒絕(rejected),回傳值也將被忽略。</dd>
+</dl>
+
+<h2 id="描述">描述</h2>
+
+<p><code><strong>Promise</strong></code> 會代理一個建立時,不用預先得知的值。它使你能夠繫結(associate)著發動非同步操作後,最終的成功值(success value)或失敗訊息(failure reason)的處理函式(handlers)。這讓非同步方法回傳值的方式很像同步方法,但不是回傳最終結果:非同步方法回傳一個 <em>promise</em> 物件作為未來某時間點的值。</p>
+
+<p>一個 <code>Promise</code> 物件處於以下幾種狀態:</p>
+
+<ul>
+ <li><em>擱置(pending)</em>:初始狀態,不是 fulfilled 與 rejected。</li>
+ <li><em>實現(fulfilled)</em>:表示操作成功地完成。</li>
+ <li><em>拒絕(rejected)</em>:表示操作失敗了。</li>
+</ul>
+
+<p>一個處於擱置狀態的 promise 能以一個值被實現(fulfilled),或是以一個原因或錯誤而被拒絕(rejected)。當上述任一狀態轉換發生時,那些透過 <code>then</code> 方法所繫結(associated)的處理函式列隊就會依序被調用。(若一個 promise 已被實現或拒絕,繫結(attached)於它的處理函式將立即被呼叫,因此完成非同步操作與繫結處理函式之間不存在競爭條件(race condition)。)</p>
+
+<p>由於 <code>{{jsxref("Promise.then", "Promise.prototype.then()")}}</code> 以及 <code>{{jsxref("Promise.catch", "Promise.prototype.catch()")}}</code> 方法都回傳 promise,它們可以被串接。</p>
+
+<p><img alt="" src="https://cdn.rawgit.com/Vectaio/a76330b025baf9bcdf07cb46e5a9ef9e/raw/26c4213a93dee1c39611dcd0ec12625811b20a26/js-promise.svg"></p>
+
+<div class="note">
+<p><strong>容易混淆:</strong> 許多其他語言擁有機制用來惰性求值(lazy evaluation)及延遲(deferring)運算,它們也被稱作“promises” — e.g. Scheme. 然而在 JavaScript 中 Promises 代表那些(已經)發生中(happening)的程序,它們可以繫結回呼函式。若您要找的是惰性求值表示式,考慮不帶參數的 <a href="/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions">arrow function</a>:<code>f = () =&gt; <em>expression</em></code> 來建立惰性求值表示式,並透過 <code>f()</code> 進行求值.</p>
+</div>
+
+<div class="note">
+<p><strong>Note</strong>: 一個被實現或拒絕,但不處於 pending 的 promise 被稱作被解決(settled)。您也會見到使用解決(resolved)一詞來描述 promises — 這代表 promises 被實現(fulfilled)了。<a href="https://github.com/domenic/promises-unwrapping/blob/master/docs/states-and-fates.md">States and fates</a> 這篇文章包含了更多 promises 的專有名詞。</p>
+</div>
+
+<h2 id="屬性">屬性</h2>
+
+<dl>
+ <dt><code>Promise.length</code></dt>
+ <dd>長度屬性,值固定為 <code>1</code>。(建構式參數數目).</dd>
+ <dt>{{jsxref("Promise.prototype")}}</dt>
+ <dd><code>Promise</code> 建構式的原型(prototype).</dd>
+</dl>
+
+<h2 id="方法">方法</h2>
+
+<dl>
+ <dt>{{jsxref("Promise.all", "Promise.all(iterable)")}}</dt>
+ <dd>回傳一個 promise,當在引數 iterable 中所有 promises 都被實現時被實現,或在引數 iterable 中有一個 promise 被拒絕時立刻被拒絕。若回傳的 promise 被實現,它將以一個實現值的陣列被實現,其順序與 iterable 中的 promises 相同。若回傳的 promise 被拒絕,它將以失敗訊息被拒絕,此訊息來自第一個在 iterable 中被拒絕的 promise。這個方法在聚集許多 promises 的結果時很有效。</dd>
+ <dt>{{jsxref("Promise.race", "Promise.race(iterable)")}}</dt>
+ <dd>回傳一個被實現或拒絕的 promise,當 iterable 中有一個 promise 被實現或拒絕時。</dd>
+</dl>
+
+<dl>
+ <dt>{{jsxref("Promise.reject", "Promise.reject(reason)")}}</dt>
+ <dd>回傳一個以失敗訊息拒絕的 <code>promise</code>。</dd>
+</dl>
+
+<dl>
+ <dt>{{jsxref("Promise.resolve", "Promise.resolve(value)")}}</dt>
+ <dd>回傳一個以 value 實現的 <code>promise</code>。若該值為 thenable (i.e. 具有 <code>then</code> 方法),回傳的 promise 將跟隨(follow)之,採用她的最終狀態; 在其他情形回傳的 promise 將以 value 被實現。一般來說,當您不知道 value 是否為 promise,使用 {{jsxref("Promise.resolve", "Promise.resolve(value)")}},將回傳值以 promise 作處理。</dd>
+</dl>
+
+<h2 id="Promise_原型"><code>Promise</code> 原型</h2>
+
+<h3 id="屬性_2">屬性</h3>
+
+<p>{{page('zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Promise/prototype','屬性')}}</p>
+
+<h3 id="方法_2">方法</h3>
+
+<p>{{page('zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Promise/prototype','方法')}}</p>
+
+<h2 id="建立_Promise">建立 Promise</h2>
+
+<p><code><font face="Open Sans, arial, x-locale-body, sans-serif">一個</font></code> <code>Promise</code> 物件透過 <code>new</code> 及其建構式建立。這個建構式接收一個叫作”執行器函式(executor function)“的引數。此函式接收兩個函式作為引數。第一個函式(<code>resolve)</code>在非同步作業成功完成時,以該作業之結果值被呼叫。第二個函式(<code>reject</code>)在作業失敗時,以失敗訊息,通常是一個 error object,被呼叫。</p>
+
+<pre class="brush: js">const myFirstPromise = new Promise((resolve, reject) =&gt; {
+ // 執行一些非同步作業,最終呼叫:
+ //
+ // resolve(someValue); // 實現
+ // 或
+ // reject("failure reason"); // 拒絕
+});
+</pre>
+
+<p>要提供一個函式 promise 功能,讓它回傳一個 promise 即可:</p>
+
+<pre class="brush: js">function myAsyncFunction(url) {
+ return new Promise((resolve, reject) =&gt; {
+ const xhr = new XMLHttpRequest();
+ xhr.open("GET", url);
+ xhr.onload = () =&gt; resolve(xhr.responseText);
+ xhr.onerror = () =&gt; reject(xhr.statusText);
+ xhr.send();
+ });
+};</pre>
+
+<h2 id="範例">範例</h2>
+
+<h3 id="入門範例">入門範例</h3>
+
+<pre class="brush: js">let myFirstPromise = new Promise((resolve, reject) =&gt; {
+ // 當非同步作業成功時,呼叫 resolve(...),而失敗時則呼叫 reject(...)。
+ // 在這個例子中,使用 setTimeout(...) 來模擬非同步程式碼。
+ // 在實務中,您將可能使用像是 XHR 或者一個 HTML5 API.
+ setTimeout(function(){
+ resolve("Success!"); // Yay!非常順利!
+ }, 250);
+});
+
+myFirstPromise.then((successMessage) =&gt; {
+ // successMessage 是任何您由上方 resolve(...) 傳入的東西。
+ // 在此僅作為成功訊息,但是它不一定是字串。
+ console.log("Yay! " + successMessage);
+});
+</pre>
+
+<h3 id="進階範例">進階範例</h3>
+
+<pre class="brush: html hidden">&lt;button id="btn"&gt;Make a promise!&lt;/button&gt;
+&lt;div id="log"&gt;&lt;/div&gt;
+</pre>
+
+<p><code>這個小範例演示了</code> <code>Promise</code> <code>的運作機制。每當 </code>{{HTMLElement("button")}} 被點擊時,<code>testPromise()</code> 方法被呼叫。每次點擊將透過 {{domxref("window.setTimeout()")}} 建立一個將在 1-3 秒內隨機地被實現的 promise,供 promise 計數(一個從 1 開始的數值)。建構式 <code>Promise()</code> 被用來建立 promise。</p>
+
+<p>promise 的實現值單純地經由一個實現回呼函式 {{jsxref("Promise.prototype.then()","p1.then()")}} 被印出。下以一些文字紀錄來展現方法中同步的與非同步處理 promise 的部分是如何分離彼此。</p>
+
+<pre class="brush: js">'use strict';
+var promiseCount = 0;
+
+function testPromise() {
+ let thisPromiseCount = ++promiseCount;
+
+ let log = document.getElementById('log');
+ log.insertAdjacentHTML('beforeend', thisPromiseCount +
+ ') Started (&lt;small&gt;Sync code started&lt;/small&gt;)&lt;br/&gt;');
+
+ // 建立一個新的 promise:此 promise 承諾一個數值計數, 由 1 開始(等待約 2 秒)
+ let p1 = new Promise(
+ // 這個解決器函數(resolver function)呼叫實現或
+ // 拒絕 promise。
+ (resolve, reject) =&gt; {
+ log.insertAdjacentHTML('beforeend', thisPromiseCount +
+ ') Promise started (&lt;small&gt;Async code started&lt;/small&gt;)&lt;br/&gt;');
+ // 在此例子單純用來產生非同步特性。
+ window.setTimeout(
+ function() {
+ // 實現這個 promise!
+ resolve(thisPromiseCount);
+ }, Math.random() * 2000 + 1000);
+ }
+ );
+
+ // 接著透過呼叫 then() 來決定 promise 進入 resolved 時,要透過 then() 做什麼,
+ // 或是進入 rejected 時,要透過 catch() 方法要做什麼。
+ p1.then(
+ // 印出實現值(fulfillment value)
+ function(val) {
+ log.insertAdjacentHTML('beforeend', val +
+ ') Promise fulfilled (&lt;small&gt;Async code terminated&lt;/small&gt;)&lt;br/&gt;');
+ })
+ .catch(
+ // 印出失敗訊息(rejection reason)
+ (reason) =&gt; {
+ console.log('Handle rejected promise ('+reason+') here.');
+ });
+
+ log.insertAdjacentHTML('beforeend', thisPromiseCount +
+ ') Promise made (&lt;small&gt;Sync code terminated&lt;/small&gt;)&lt;br/&gt;');
+}</pre>
+
+<p><sub>*譯註:resolver function 即 executor function。</sub></p>
+
+<pre class="brush:js hidden">if ("Promise" in window) {
+ let btn = document.getElementById("btn");
+ btn.addEventListener("click",testPromise);
+} else {
+ log = document.getElementById('log');
+ log.innerHTML = "Live example not available as your browser doesn't support the &lt;code&gt;Promise&lt;code&gt; interface.";
+}
+</pre>
+
+<p>這個範例從點擊按鈕開始。您的瀏覽器需要支援 Promise。在短時間內點擊按鈕許多次,您甚至將看到不同的 promises 一個接一個地被實現。</p>
+
+<p>{{EmbedLiveSample("Advanced_Example", "500", "200")}}</p>
+
+<h2 id="使用_XHR_載入圖片">使用 XHR 載入圖片</h2>
+
+<p>另一個使用 <code>Promise</code> and <code><a href="/zh-TW/docs/Web/API/XMLHttpRequest">XMLHttpRequest</a></code> 來載入圖片的簡單例子可以在 MDN GitHub <a href="https://github.com/mdn/js-examples/tree/master/promises-test">js-examples</a> 儲存庫找到。 你也可以<a href="https://mdn.github.io/js-examples/promises-test/">see it in action</a>。每個步驟都附以註解,讓你能逐步遵隨 Promise 與 XHR 架構。</p>
+
+<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('ES2015', '#sec-promise-objects', 'Promise')}}</td>
+ <td>{{Spec2('ES2015')}}</td>
+ <td>Initial definition in an ECMA standard.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-promise-objects', 'Promise')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
+
+<p class="hidden">To contribute to this compatibility data, please write a pull request against this repository: <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a>.</p>
+
+<p>{{Compat("javascript.builtins.Promise")}}</p>
+
+<h2 id="參見">參見</h2>
+
+<ul>
+ <li><a href="/zh-TW/docs/Web/JavaScript/Guide/Using_promises">Using promises</a></li>
+ <li><a href="http://promisesaplus.com/">Promises/A+ specification</a></li>
+ <li><a href="https://medium.com/@ramsunvtech/promises-of-promise-part-1-53f769245a53">Venkatraman.R - JS Promise (Part 1, Basics)</a></li>
+ <li><a href="https://medium.com/@ramsunvtech/js-promise-part-2-q-js-when-js-and-rsvp-js-af596232525c#.dzlqh6ski">Venkatraman.R - JS Promise (Part 2 - Using Q.js, When.js and RSVP.js)</a></li>
+ <li><a href="https://tech.io/playgrounds/11107/tools-for-promises-unittesting/introduction">Venkatraman.R - Tools for Promises Unit Testing</a></li>
+ <li><a href="http://www.html5rocks.com/en/tutorials/es6/promises/">Jake Archibald: JavaScript Promises: There and Back Again</a></li>
+ <li><a href="http://de.slideshare.net/domenicdenicola/callbacks-promises-and-coroutines-oh-my-the-evolution-of-asynchronicity-in-javascript">Domenic Denicola: Callbacks, Promises, and Coroutines – Asynchronous Programming Patterns in JavaScript</a></li>
+ <li><a href="http://www.mattgreer.org/articles/promises-in-wicked-detail/">Matt Greer: JavaScript Promises ... In Wicked Detail</a></li>
+ <li><a href="https://www.promisejs.org/">Forbes Lindesay: promisejs.org</a></li>
+ <li><a href="http://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html">Nolan Lawson: We have a problem with promises — Common mistakes with promises</a></li>
+ <li><a href="https://github.com/jakearchibald/es6-promise/">Promise polyfill</a></li>
+ <li><a href="https://www.udacity.com/course/javascript-promises--ud898">Udacity: JavaScript Promises</a></li>
+</ul>
diff --git a/files/zh-tw/web/javascript/reference/global_objects/promise/prototype/index.html b/files/zh-tw/web/javascript/reference/global_objects/promise/prototype/index.html
new file mode 100644
index 0000000000..476c52f7a3
--- /dev/null
+++ b/files/zh-tw/web/javascript/reference/global_objects/promise/prototype/index.html
@@ -0,0 +1,64 @@
+---
+title: Promise.prototype
+slug: Web/JavaScript/Reference/Global_Objects/Promise/prototype
+translation_of: Web/JavaScript/Reference/Global_Objects/Promise
+---
+<div>{{JSRef}}</div>
+
+<p><code><strong>Promise</strong></code><strong><code>.prototype</code></strong> 屬性代表了 {{jsxref("Promise")}} 建構式的原型物件。</p>
+
+<div>{{js_property_attributes(0,0,0)}}</div>
+
+<h2 id="描述">描述</h2>
+
+<p>所有 {{jsxref("Promise")}} 實例都繼承自 {{jsxref("Promise.prototype")}}。您可以使用建構式的原型物件來增加屬性或方法到所有的 <code>Promise</code> 實例。</p>
+
+<h2 id="屬性">屬性</h2>
+
+<dl>
+ <dt><code>Promise.prototype.constructor</code></dt>
+ <dd>回傳一個建立實例原型(instance's prototype)的函式。預設為 {{jsxref("Promise")}} 函數。</dd>
+</dl>
+
+<h2 id="方法">方法</h2>
+
+<dl>
+ <dt>{{jsxref("Promise.catch", "Promise.prototype.catch(onRejected)")}}</dt>
+ <dd>繫結一個拒絕回呼函式(rejection handler callback)到 promise,當它被呼叫時回傳一個以回傳值作解析的新 promise,或者當 promise 被實現時以原值作解析。</dd>
+ <dt>{{jsxref("Promise.then", "Promise.prototype.then(onFulfilled, onRejected)")}}</dt>
+ <dd>繫結實現或拒絕回呼函式到 promise,回傳一個以 handler 之回傳值作解析的新 promise,或者當 promise 未處理(not handled)時以原值作解析。(i.e. 比如相關聯的 <code>onFulfilled</code> 或 <code>onRejected</code> 不是函式。)</dd>
+</dl>
+
+<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.prototype', 'Promise.prototype')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td>Initial definition.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-promise.prototype', 'Promise.prototype')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
+
+<p class="hidden">To contribute to this compatibility data, please write a pull request against this file: <a href="https://github.com/mdn/browser-compat-data/blob/master/javascript/promise.json">https://github.com/mdn/browser-compat-data/blob/master/javascript/promise.json</a>.</p>
+
+<p>{{Compat("javascript/promise","Promise.prototype")}}</p>
+
+<h2 id="參見">參見</h2>
+
+<ul>
+ <li>{{jsxref("Promise")}}</li>
+</ul>
diff --git a/files/zh-tw/web/javascript/reference/global_objects/promise/race/index.html b/files/zh-tw/web/javascript/reference/global_objects/promise/race/index.html
new file mode 100644
index 0000000000..9f0d8b4d2e
--- /dev/null
+++ b/files/zh-tw/web/javascript/reference/global_objects/promise/race/index.html
@@ -0,0 +1,171 @@
+---
+title: Promise.race()
+slug: Web/JavaScript/Reference/Global_Objects/Promise/race
+translation_of: Web/JavaScript/Reference/Global_Objects/Promise/race
+---
+<div>{{JSRef}}</div>
+
+<p><code><strong>Promise.race(iterable)</strong></code> 方法回傳一個 promise 物件,此 promise 物件會於 iterable 引數中任一個 promise 轉為 resolve 或 rejected 時立即轉變成 resolve 或 rejected,並且接收其成功值或失敗訊息。</p>
+
+<h2 id="語法">語法</h2>
+
+<pre class="syntaxbox"><var>Promise.race(iterable)</var>;</pre>
+
+<h3 id="參數">參數</h3>
+
+<dl>
+ <dt>iterable</dt>
+ <dd>一個 iterable 物件,像是 {{jsxref("Array")}}. 請參考<a href="/zh-TW/docs/Web/JavaScript/Reference/Iteration_protocols#可迭代協議">可迭代協議</a>。</dd>
+</dl>
+
+<h3 id="回傳值">回傳值</h3>
+
+<p>當傳入的 iterable 中有 promise 被實現或拒絕時,立刻回傳被實現或拒絕的 {{jsxref("Promise")}}。</p>
+
+<h2 id="描述">描述</h2>
+
+<p><code>race</code> 函式回傳一個與傳入的 iterable 之中第一個被解決(settled)的 promise 相同方式被解決(且以相同值)的 <code>Promise</code>。</p>
+
+<h2 id="範例">範例</h2>
+
+<h3 id="Promise.race_的非同步性質"><font face="consolas, Liberation Mono, courier, monospace"><code>Promise.race</code> 的非同步性質</font></h3>
+
+<p><code><font face="Open Sans, arial, x-locale-body, sans-serif">以下例子演示了 </font>Promise.race</code> <code>的非同步性質:</code></p>
+
+<pre class="brush: js">// we are passing as argument an array of promises that are already resolved,
+// to trigger Promise.race as soon as possible
+var resolvedPromisesArray = [Promise.resolve(33), Promise.resolve(44)];
+
+var p = Promise.race(resolvedPromisesArray);
+// immediately logging the value of p
+console.log(p);
+
+// using setTimeout we can execute code after the stack is empty
+setTimeout(function(){
+ console.log('the stack is now empty');
+ console.log(p);
+});
+
+// logs, in order:
+// Promise { &lt;state&gt;: "pending" }
+// the stack is now empty
+// Promise { &lt;state&gt;: "fulfilled", &lt;value&gt;: 33 }</pre>
+
+<p>一個空的 iterable 造成回傳的 promise 永久擱置:</p>
+
+<pre class="brush: js">var foreverPendingPromise = Promise.race([]);
+console.log(foreverPendingPromise);
+setTimeout(function(){
+ console.log('the stack is now empty');
+ console.log(foreverPendingPromise);
+});
+
+// logs, in order:
+// Promise { &lt;state&gt;: "pending" }
+// the stack is now empty
+// Promise { &lt;state&gt;: "pending" }
+</pre>
+
+<p>若 iterable 中有一個或多個非 promise 值且/或一個已經被實現/解決的 promise,<code>Promise.race</code> 將以陣列中第一個這樣的值解決:</p>
+
+<pre class="brush: js">var foreverPendingPromise = Promise.race([]);
+var alreadyResolvedProm = Promise.resolve(666);
+
+var arr = [foreverPendingPromise, alreadyResolvedProm, "non-Promise value"];
+var arr2 = [foreverPendingPromise, "non-Promise value", Promise.resolve(666)];
+var p = Promise.race(arr);
+var p2 = Promise.race(arr2);
+
+console.log(p);
+console.log(p2);
+setTimeout(function(){
+    console.log('the stack is now empty');
+    console.log(p);
+ console.log(p2);
+});
+
+// logs, in order:
+// Promise { &lt;state&gt;: "pending" }
+// Promise { &lt;state&gt;: "pending" }
+// the stack is now empty
+// Promise { &lt;state&gt;: "fulfilled", &lt;value&gt;: 666 }
+// Promise { &lt;state&gt;: "fulfilled", &lt;value&gt;: "non-Promise value" }
+</pre>
+
+<h3 id="使用_Promise.race_–_及_setTimeout_的範例">使用 <code>Promise.race</code> – 及 <code><a href="/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout">setTimeout</a></code> 的範例</h3>
+
+<pre class="brush: js">var p1 = new Promise(function(resolve, reject) {
+ setTimeout(resolve, 500, 'one');
+});
+var p2 = new Promise(function(resolve, reject) {
+ setTimeout(resolve, 100, 'two');
+});
+
+Promise.race([p1, p2]).then(function(value) {
+ console.log(value); // "two"
+ // Both resolve, but p2 is faster
+});
+
+var p3 = new Promise(function(resolve, reject) {
+ setTimeout(resolve, 100, 'three');
+});
+var p4 = new Promise(function(resolve, reject) {
+ setTimeout(reject, 500, 'four');
+});
+
+Promise.race([p3, p4]).then(function(value) {
+ console.log(value); // "three"
+ // p3 is faster, so it resolves
+}, function(reason) {
+ // Not called
+});
+
+var p5 = new Promise(function(resolve, reject) {
+ setTimeout(resolve, 500, 'five');
+});
+var p6 = new Promise(function(resolve, reject) {
+ setTimeout(reject, 100, 'six');
+});
+
+Promise.race([p5, p6]).then(function(value) {
+ // Not called
+}, function(reason) {
+ console.log(reason); // "six"
+ // p6 is faster, so it rejects
+});
+</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('ES2015', '#sec-promise.race', 'Promise.race')}}</td>
+ <td>{{Spec2('ES2015')}}</td>
+ <td>Initial definition in an ECMA standard.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-promise.race', 'Promise.race')}}</td>
+ <td>{{Spec2('ESDraft')}}</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.race")}}</p>
+
+<h2 id="參見">參見</h2>
+
+<ul>
+ <li>{{jsxref("Promise")}}</li>
+ <li>{{jsxref("Promise.all()")}}</li>
+</ul>
diff --git a/files/zh-tw/web/javascript/reference/global_objects/promise/reject/index.html b/files/zh-tw/web/javascript/reference/global_objects/promise/reject/index.html
new file mode 100644
index 0000000000..0c41a37509
--- /dev/null
+++ b/files/zh-tw/web/javascript/reference/global_objects/promise/reject/index.html
@@ -0,0 +1,72 @@
+---
+title: Promise.reject()
+slug: Web/JavaScript/Reference/Global_Objects/Promise/reject
+translation_of: Web/JavaScript/Reference/Global_Objects/Promise/reject
+---
+<div>{{JSRef}}</div>
+
+<p><code><strong>Promise.reject(reason)</strong></code><strong> </strong>方法回傳一個以 <code>reason</code> 拒絕的 <code>Promise</code> 物件。</p>
+
+<h2 id="語法">語法</h2>
+
+<pre class="syntaxbox"><var>Promise.reject(reason)</var>;</pre>
+
+<h3 id="參數">參數</h3>
+
+<dl>
+ <dt>reason</dt>
+ <dd><code>Promise</code> 的失敗訊息。</dd>
+</dl>
+
+<h3 id="回傳值">回傳值</h3>
+
+<p>一個以 <code>reason</code> 拒絕的 {{jsxref("Promise")}}。</p>
+
+<h2 id="描述">描述</h2>
+
+<p>靜態函式 <code>Promise.reject</code> 回傳一個被拒絕的 <code>Promise。由於除錯目的及選擇性錯誤捕捉(selective error catching),使</code>用一個 <code>instanceof</code> {{jsxref("Error")}} 作為 reason 是很有幫助的。</p>
+
+<h2 id="範例">範例</h2>
+
+<h3 id="使用靜態方法_Promise.reject()">使用靜態方法 Promise.reject()</h3>
+
+<pre class="brush: js">Promise.reject(new Error('fail')).then(function(error) {
+ // not called
+}, function(error) {
+ console.log(error); // Stacktrace
+});</pre>
+
+<h2 id="Specifications">Specifications</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('ES2015', '#sec-promise.reject', 'Promise.reject')}}</td>
+ <td>{{Spec2('ES2015')}}</td>
+ <td>Initial definition in an ECMA standard.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-promise.reject', 'Promise.reject')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility">Browser compatibility</h2>
+
+<p class="hidden">To contribute to this compatibility data, please write a pull request against this repository: <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a>.</p>
+
+<p>{{Compat("javascript.builtins.Promise.reject")}}</p>
+
+<h2 id="See_also">See also</h2>
+
+<ul>
+ <li>{{jsxref("Promise")}}</li>
+ <li><a href="https://github.com/petkaantonov/bluebird#error-handling">Selective error catching using the BlueBird Promise library</a></li>
+</ul>
diff --git a/files/zh-tw/web/javascript/reference/global_objects/promise/resolve/index.html b/files/zh-tw/web/javascript/reference/global_objects/promise/resolve/index.html
new file mode 100644
index 0000000000..e2d460a7e3
--- /dev/null
+++ b/files/zh-tw/web/javascript/reference/global_objects/promise/resolve/index.html
@@ -0,0 +1,142 @@
+---
+title: Promise.resolve()
+slug: Web/JavaScript/Reference/Global_Objects/Promise/resolve
+translation_of: Web/JavaScript/Reference/Global_Objects/Promise/resolve
+---
+<div>{{JSRef}}</div>
+
+<p><code><strong>Promise.resolve(value)</strong></code> 方法回傳一個以 value 判定結果的 {{jsxref("Promise")}} 物件。若 value 是個 thenable (例如,具有 {{jsxref("Promise.then", "\"then\"方法")}}),則回傳的 promise 將依其結果採取其最終狀態;若 value 是 promise,則作為呼叫 Promise.resolve 之結果;其他情形都將回傳以 value 實現的 promise。</p>
+
+<h2 id="語法">語法</h2>
+
+<pre class="brush: js">Promise.resolve(value);
+Promise.resolve(promise);
+Promise.resolve(thenable);
+</pre>
+
+<h3 id="參數">參數</h3>
+
+<dl>
+ <dt>value</dt>
+ <dd>將被 <code>Promise</code> 實現的引數(argument)。可以是個 <code>Promise</code> 或待解決的 thenable。</dd>
+</dl>
+
+<h3 id="回傳值">回傳值</h3>
+
+<p>以 value 或作為 value 的 promise 解決的 {{jsxref("Promise")}}。</p>
+
+<h2 id="描述">描述</h2>
+
+<p><code>靜態函式</code> <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) {
+ // not called
+});
+</pre>
+
+<h3 id="判定陣列">判定陣列</h3>
+
+<pre class="brush: js">var p = Promise.resolve([1,2,3]);
+p.then(function(v) {
+ console.log(v[0]); // 1
+});
+</pre>
+
+<h3 id="判定另一個_Promise">判定另一個 <code>Promise</code></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));
+
+// logs, in order:
+// original === cast ? true
+// value: 33
+</pre>
+
+<p>由於 handlers 是非同步地被調用而導致相反的紀錄順序。經由<a href="https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#回傳值">這篇文章</a>了解 then 如何運作。</p>
+
+<h3 id="判定_thenables_及拋出_Errors">判定 thenables 及拋出 Errors</h3>
+
+<pre class="brush: js">// Resolving a thenable object
+var p1 = Promise.resolve({
+ then: function(onFulfill, onReject) { onFulfill('fulfilled!'); }
+});
+console.log(p1 instanceof Promise) // true, object casted to a Promise
+
+p1.then(function(v) {
+ console.log(v); // "fulfilled!"
+ }, function(e) {
+ // not called
+});
+
+// Thenable throws before callback
+// Promise rejects
+var thenable = { then: function(resolve) {
+ throw new TypeError('Throwing');
+ resolve('Resolving');
+}};
+
+var p2 = Promise.resolve(thenable);
+p2.then(function(v) {
+ // not called
+}, function(e) {
+ console.log(e); // TypeError: Throwing
+});
+
+// Thenable throws after 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) {
+ // not called
+});
+</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('ES2015', '#sec-promise.resolve', 'Promise.resolve')}}</td>
+ <td>{{Spec2('ES2015')}}</td>
+ <td>Initial definition in an ECMA standard.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-promise.resolve', 'Promise.resolve')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
+
+<p class="hidden">To contribute to this compatibility data, please write a pull request against this repository: <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a>.</p>
+
+<p>{{Compat("javascript.builtins.Promise.resolve")}}</p>
+
+<h2 id="參見">參見</h2>
+
+<ul>
+ <li>{{jsxref("Promise")}}</li>
+</ul>
diff --git a/files/zh-tw/web/javascript/reference/global_objects/promise/then/index.html b/files/zh-tw/web/javascript/reference/global_objects/promise/then/index.html
new file mode 100644
index 0000000000..19682d0199
--- /dev/null
+++ b/files/zh-tw/web/javascript/reference/global_objects/promise/then/index.html
@@ -0,0 +1,271 @@
+---
+title: Promise.prototype.then()
+slug: Web/JavaScript/Reference/Global_Objects/Promise/then
+translation_of: Web/JavaScript/Reference/Global_Objects/Promise/then
+---
+<div>{{JSRef}}</div>
+
+<p><strong><code>then()</code> </strong>方法回傳一個 {{domxref("Promise")}} 物件。它接收兩個引數: <code>Promise</code> 在成功及失敗情況時的回呼函式。</p>
+
+<div class="note">
+<p>如果有一個或兩個引數被省略,或為非函式(non-functions),則 <code>then</code> 將處於遺失 handler(s) 的狀態,但不會產生錯誤。若發起 <code>then</code> 之 <code>Promise</code> 採取了一個狀態(實現(<code>fulfillment)</code>或拒絕(<code>rejection))</code>而 <code>then</code> 沒有處理它的函式,一個不具有額外 handlers 的新 <code>Promise</code> 物件將被建立,單純採取原 <code>Promise</code> 其最終狀態。</p>
+</div>
+
+<h2 id="語法">語法</h2>
+
+<pre class="syntaxbox"><var>p.then(onFulfilled[, onRejected])</var>;
+
+p.then(function(value) {
+ // fulfillment
+}, function(reason) {
+ // rejection
+});
+</pre>
+
+<h3 id="參數">參數</h3>
+
+<dl>
+ <dt><code>onFulfilled</code></dt>
+ <dd>一個 {{jsxref("Function")}},當 <code>Promise</code> 被實現(fulfilled)時被呼叫。此函式接收一個實現值(<code>fullfillment value)作為引數。</code></dd>
+ <dt><code>onRejected </code>{{optional_inline}}</dt>
+ <dd>一個 {{jsxref("Function")}},當 <code>Promise</code> 被拒絕(rejected)時被呼叫。此函式接收一個失敗訊息(<code>rejection reason)作為引數。</code></dd>
+</dl>
+
+<h3 id="回傳值">回傳值</h3>
+
+<p>一個進入<strong>擱置(pending)</strong>狀態的 {{jsxref("Promise")}}。(只要堆疊一空)handler 函式<strong>非同步地(asynchronously)</strong>被呼叫。在調用 handler 後,若 handler 函式:</p>
+
+<ul>
+ <li>回傳一個值,則 <code>then</code> 回傳之 promise 以此值被實現(resolved)。</li>
+ <li>拋出一個例外,則 <code>then</code> 回傳之 promise 以此例外被否決(rejected)。</li>
+ <li>回傳一個被實現的 promise,則 <code>then</code> 回傳之 promise 以此值被實現。</li>
+ <li>回傳一個被否決的 promise,則 <code>then</code> 回傳之 promise 以此值被否決。</li>
+ <li>回傳另一個被<strong>擱置</strong>的 promise 物件,則 <code>then</code> 回傳之 promise 之實現/拒絕隨後由處理函式之實現/否決決定。並且,<code>then</code> 回傳之 promise 將與處理函式回傳之 promise 以相同值被解決。</li>
+</ul>
+
+<p>以下例子展示 <code>then</code> 方法的非同步性質(asynchronicity)。</p>
+
+<pre class="brush: js">// 使用一個已實現的 promise,'then' 區塊將立即被觸發,但是它的 handlers 將是非同步地被觸發,如同 console.logs 所示
+var resolvedProm = Promise.resolve(33);
+
+var thenProm = resolvedProm.then(function(value){
+ console.log("我在 main stack 之後被呼叫。收到及將回傳的值為:" + value);
+ return value;
+});
+// 立即紀錄 thenProm
+console.log(thenProm);
+
+// 我們可以使用 setTimeout 以延遲(postpone)函式執行直到堆疊為空
+setTimeout(function(){
+    console.log(thenProm);
+});
+
+
+// 紀錄結果,依序為:
+// Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
+// "我在 main stack 之後被呼叫。收到及將回傳的值為:33"
+// Promise {[[PromiseStatus]]: "resolved", [[PromiseValue]]: 33}
+</pre>
+
+<h2 id="描述">描述</h2>
+
+<p>因為 <code>then</code> 和 {{jsxref("Promise.prototype.catch()")}} 方法都回傳 promises,它們可以被串接 — 稱為組合(<em>composition)。</em></p>
+
+<h2 id="範例">範例</h2>
+
+<h3 id="運用_then_方法">運用 <code>then</code> 方法</h3>
+
+<pre class="brush: js">var p1 = new Promise( (resolve, reject) =&gt; {
+ resolve('Success!');
+ // or
+ // reject ("Error!");
+} );
+
+p1.then( value =&gt; {
+ console.log(value); // Success!
+}, reason =&gt; {
+ console.log(reason); // Error!
+} );
+</pre>
+
+<h3 id="串接">串接</h3>
+
+<p><code>then</code> 方法回傳一個 <code>Promise</code> 而可以進行方法串接(method chaining)。</p>
+
+<p>如果傳入 <code>then</code> 的 handler 函式回傳一個 promise,一個等價的 <code>Promise</code> 將被展現給方法串接中的下一個 then 。以下程式碼片段透過 <code>setTimout</code> 函式模擬非同步程式碼。</p>
+
+<pre class="brush: js">Promise.resolve('foo')
+ // 1. Receive "foo" concatenate "bar" to it and resolve that to the next then
+ .then(function(string) {
+ return new Promise(function(resolve, reject) {
+ setTimeout(function() {
+ string += 'bar';
+ resolve(string);
+ }, 1);
+ });
+ })
+ // 2. receive "foobar", register a callback function to work on that string
+ // and print it to the console, but not before return the unworked on
+ // string to the next then
+ .then(function(string) {
+ setTimeout(function() {
+ string += 'baz';
+ console.log(string);
+ }, 1)
+ return string;
+ })
+ // 3. print helpful messages about how the code in this section will be run
+ // before string is actually processed by the mocked asynchronous code in the
+ // prior then block.
+ .then(function(string) {
+ console.log("Last Then: oops... didn't bother to instantiate and return " +
+ "a promise in the prior then so the sequence may be a bit " +
+ "surprising");
+
+ // Note that `string` will not have the 'baz' bit of it at this point. This
+ // is because we mocked that to happen asynchronously with a setTimeout function
+ console.log(string);
+ });</pre>
+
+<p>當 handler 僅回傳一個值,實際上它將回傳 <code>Promise.resolve(&lt;value returned by whichever handler was called&gt;)</code>.</p>
+
+<pre class="brush: js">var p2 = new Promise(function(resolve, reject) {
+ resolve(1);
+});
+
+p2.then(function(value) {
+ console.log(value); // 1
+ return value + 1;
+}).then(function(value) {
+ console.log(value + '- This synchronous usage is virtually pointless'); // 2- This synchronous usage is virtually pointless
+});
+
+p2.then(function(value) {
+ console.log(value); // 1
+});
+</pre>
+
+<p>若函式拋出一個錯誤或回傳一個被否決的 Promise,<code>then</code> 也將回傳一個被否決的 Promise。</p>
+
+<pre class="brush: js">Promise.resolve()
+ .then( () =&gt; {
+ // 使 .then() 回傳一個被否決的 Promise
+ throw 'Oh no!';
+ })
+ .then( () =&gt; {
+ console.log( 'Not called.' );
+ }, reason =&gt; {
+ console.error( 'onRejected function called: ', reason );
+ });</pre>
+
+<p>在所有其他情形,實現中的 Promise 被回傳。在以下例子中,第一個 <code>then()</code> 將回傳一個實現中包裹 42 的 promise,即使串接中的前一個 Promise 被否決。</p>
+
+<pre class="brush: js">Promise.reject()
+ .then( () =&gt; 99, () =&gt; 42 ) // onRejected returns 42 which is wrapped in a resolving Promise
+ .then( solution =&gt; console.log( 'Resolved with ' + solution ) ); // Resolved with 42</pre>
+
+<p>實務上,使用 <code>catch</code> 捕捉被否決的 promise 較理想的,而不建議使用兩個引數 <code>then</code> 語法,如下展示。</p>
+
+<pre class="brush: js">Promise.resolve()
+ .then( () =&gt; {
+ // Makes .then() return a rejected promise
+ throw 'Oh no!';
+ })
+ .catch( reason =&gt; {
+ console.error( 'onRejected function called: ', reason );
+ })
+ .then( () =&gt; {
+ console.log( "I am always called even if the prior then's promise rejects" );
+ });</pre>
+
+<p><br>
+ 你也可以透過串接實作一個 Promise-based API 函式,基於它本身。</p>
+
+<pre class="brush: js">function fetch_current_data() {
+ // The <a href="/en-US/docs/Web/API/GlobalFetch/fetch">fetch</a>() API returns a Promise. This function
+ // exposes a similar API, except the fulfillment
+ // value of this function's Promise has had more
+ // work done on it.
+ return fetch('current-data.json').then((response) =&gt; {
+ if (response.headers.get('content-type') != 'application/json') {
+ throw new TypeError();
+ }
+ var j = response.json();
+ // maybe do something with j
+ return j; // fulfillment value given to user of
+ // fetch_current_data().then()
+ });
+}
+</pre>
+
+<p>若 <code>onFulfilled</code> 回傳一個 promise,則 <code>then</code> 的實現/否決將取決它。</p>
+
+<pre class="brush: js">function resolveLater(resolve, reject) {
+ setTimeout(function () {
+ resolve(10);
+ }, 1000);
+}
+function rejectLater(resolve, reject) {
+ setTimeout(function () {
+ reject(20);
+ }, 1000);
+}
+
+var p1 = Promise.resolve('foo');
+var p2 = p1.then(function() {
+ // Return promise here, that will be resolved to 10 after 1 second
+ return new Promise(resolveLater);
+});
+p2.then(function(v) {
+ console.log('resolved', v); // "resolved", 10
+}, function(e) {
+ // not called
+ console.log('rejected', e);
+});
+
+var p3 = p1.then(function() {
+ // Return promise here, that will be rejected with 20 after 1 second
+ return new Promise(rejectLater);
+});
+p3.then(function(v) {
+ // not called
+ console.log('resolved', v);
+}, function(e) {
+ console.log('rejected', e); // "rejected", 20
+});
+</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('ES2015', '#sec-promise.prototype.then', 'Promise.prototype.then')}}</td>
+ <td>{{Spec2('ES2015')}}</td>
+ <td>Initial definition in an ECMA standard.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-promise.prototype.then', 'Promise.prototype.then')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="瀏覽器相容性">瀏覽器相容性</h2>
+
+<p class="hidden">To contribute to this compatibility data, please write a pull request against this file: <a href="https://github.com/mdn/browser-compat-data/blob/master/javascript/promise.json">https://github.com/mdn/browser-compat-data/blob/master/javascript/promise.json</a>.</p>
+
+<p>{{Compat("javascript/promise","Promise.prototype.then")}}</p>
+
+<h2 id="參見">參見</h2>
+
+<ul>
+ <li>{{jsxref("Promise")}}</li>
+ <li>{{jsxref("Promise.prototype.catch()")}}</li>
+</ul>