<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                一般情況下我們都會使用?`new Promise()`?來創建promise對象,但是除此之外我們也可以使用其他方法。 在這里,我們將會學習如何使用?[`Promise.resolve`](http://liubin.github.io/promises-book/#Promise.resolve)?和?[`Promise.reject`](http://liubin.github.io/promises-book/#Promise.reject)這兩個方法。 ## 2.1.1\. new Promise的快捷方式 靜態方法[`Promise.resolve(value)`](http://liubin.github.io/promises-book/#Promise.resolve)?可以認為是?`new Promise()`?方法的快捷方式。 比如?`Promise.resolve(42);`?可以認為是以下代碼的語法糖。 ~~~ new Promise(function(resolve){ resolve(42); }); ~~~ 在這段代碼中的?`resolve(42);`?會讓這個promise對象立即進入確定(即resolved)狀態,并將?`42`?傳遞給后面then里所指定的?`onFulfilled`?函數。 方法?`Promise.resolve(value);`?的返回值也是一個promise對象,所以我們可以像下面那樣接著對其返回值進行?`.then`?調用。 ~~~ Promise.resolve(42).then(function(value){ console.log(value); }); ~~~ [Promise.resolve](http://liubin.github.io/promises-book/#Promise.resolve)作為?`new Promise()`?的快捷方式,在進行promise對象的初始化或者編寫測試代碼的時候都非常方便。 ## 2.1.2\. Thenable `Promise.resolve`?方法另一個作用就是將?[thenable](http://liubin.github.io/promises-book/#Thenable)?對象轉換為promise對象。 > [ES6 Promises](http://liubin.github.io/promises-book/#es6-promises)里提到了[Thenable](http://liubin.github.io/promises-book/#Thenable)這個概念,簡單來說它就是一個非常類似promise的東西。 就像我們有時稱具有?`.length`?方法的非數組對象為Array like一樣,thenable指的是一個具有?`.then`?方法的對象。 這種將thenable對象轉換為promise對象的機制要求thenable對象所擁有的?`then`?方法應該和Promise所擁有的?`then`?方法具有同樣的功能和處理過程,在將thenable對象轉換為promise對象的時候,還會巧妙的利用thenable對象原來具有的?`then`?方法。 到底什么樣的對象能算是thenable的呢,最簡單的例子就是?[jQuery.ajax()](https://api.jquery.com/jQuery.ajax/),它的返回值就是thenable的。 因為`jQuery.ajax()`?的返回值是?[jqXHR Object](http://api.jquery.com/jQuery.ajax/#jqXHR)?對象,這個對象具有?`.then`?方法。 ~~~ $.ajax('/json/comment.json');// => 擁有 `.then` 方法的對象 ~~~ 這個thenable的對象可以使用?`Promise.resolve`?來轉換為一個promise對象。 變成了promise對象的話,就能直接使用?`then`?或者?`catch`?等這些在?[ES6 Promises](http://liubin.github.io/promises-book/#es6-promises)里定義的方法了。 將thenable對象轉換promise對象 ~~~ var promise = Promise.resolve($.ajax('/json/comment.json'));// => promise對象 promise.then(function(value){ console.log(value); }); ~~~ > jQuery和thenable > [jQuery.ajax()](https://api.jquery.com/jQuery.ajax/)的返回值是一個具有?`.then`?方法的?[jqXHR Object](http://api.jquery.com/jQuery.ajax/#jqXHR)對象,這個對象繼承了來自?[Deferred Object](http://api.jquery.com/category/deferred-object/)?的方法和屬性。 > 但是Deferred Object并沒有遵循[Promises/A+](http://liubin.github.io/promises-book/#promises-aplus)或[ES6 Promises](http://liubin.github.io/promises-book/#es6-promises)標準,所以即使看上去這個對象轉換成了一個promise對象,但是會出現缺失部分信息的問題。 > 這個問題的根源在于jQuery的?[Deferred Object](http://api.jquery.com/category/deferred-object/)?的?`then`?方法機制與promise不同。 > 所以我們應該注意,即使一個對象具有?`.then`?方法,也不一定就能作為ES6 Promises對象使用。 > * [JavaScript Promises: There and back again - HTML5 Rocks](http://www.html5rocks.com/en/tutorials/es6/promises/#toc-lib-compatibility) > * [You're Missing the Point of Promises](http://domenic.me/2012/10/14/youre-missing-the-point-of-promises/) > * [https://twitter.com/hirano_y_aa/status/398851806383452160](https://twitter.com/hirano_y_aa/status/398851806383452160) `Promise.resolve`?只使用了共通的方法?`then`?,提供了在不同的類庫之間進行promise對象互相轉換的功能。 這種轉換為thenable的功能在之前是通過使用?`Promise.cast`?來完成的,從它的名字我們也不難想象它的功能是什么。 除了在編寫使用Promise的類庫等軟件時需要對Thenable有所了解之外,通常作為end-user使用的時候,我們可能不會用到此功能。 > 我們會在后面第4章的[Promise.resolve和Thenable](http://liubin.github.io/promises-book/#resolve-thenable)中進行詳細的說明,介紹一下結合使用了Thenable和Promise.resolve的具體例子。 簡單總結一下?`Promise.resolve`?方法的話,可以認為它的作用就是將傳遞給它的參數填充(Fulfilled)到promise對象后并返回這個promise對象。 此外,Promise的很多處理內部也是使用了?`Promise.resolve`?算法將值轉換為promise對象后再進行處理的。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看