<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國際加速解決方案。 廣告
                接著我們來看看和?[`Promise.all`](http://liubin.github.io/promises-book/#Promise.all)?類似的對多個promise對象進行處理的?[`Promise.race`](http://liubin.github.io/promises-book/#Promise.race)?方法。 它的使用方法和Promise.all一樣,接收一個promise對象數組為參數。 `Promise.all`?在接收到的所有的對象promise都變為 FulFilled 或者 Rejected 狀態之后才會繼續進行后面的處理, 與之相對的是?`Promise.race`?只要有一個promise對象進入 FulFilled 或者 Rejected 狀態的話,就會繼續進行后面的處理。 像Promise.all時的例子一樣,我們來看一個帶計時器的?`Promise.race`?的使用例子。 promise-race-timer.js ~~~ // `delay`毫秒后執行resolve function timerPromisefy(delay) { return new Promise(function (resolve) { setTimeout(function () { resolve(delay); }, delay); }); } // 任何一個promise變為resolve或reject 的話程序就停止運行 Promise.race([ timerPromisefy(1), timerPromisefy(32), timerPromisefy(64), timerPromisefy(128) ]).then(function (value) { console.log(value); // => 1 }); ~~~ 上面的代碼創建了4個promise對象,這些promise對象會分別在1ms,32ms,64ms和128ms后變為確定狀態,即FulFilled,并且在第一個變為確定狀態的1ms后,?`.then`?注冊的回調函數就會被調用,這時候確定狀態的promise對象會調用?`resolve(1)`?因此傳遞給?`value`?的值也是1,控制臺上會打印出`1`來。 下面我們再來看看在第一個promise對象變為確定(FulFilled)狀態后,它之后的promise對象是否還在繼續運行。 promise-race-other.js ~~~ var winnerPromise = new Promise(function (resolve) { setTimeout(function () { console.log('this is winner'); resolve('this is winner'); }, 4); }); var loserPromise = new Promise(function (resolve) { setTimeout(function () { console.log('this is loser'); resolve('this is loser'); }, 1000); }); // 第一個promise變為resolve后程序停止 Promise.race([winnerPromise, loserPromise]).then(function (value) { console.log(value); // => 'this is winner' }); ~~~ 我們在前面代碼的基礎上增加了?`console.log`?用來輸出調試信息。 執行上面代碼的話,我們會看到 winnter和loser promise對象的?`setTimeout`?方法都會執行完畢,?`console.log`?也會分別輸出它們的信息。 也就是說,?[`Promise.race`](http://liubin.github.io/promises-book/#Promise.race)?在第一個promise對象變為Fulfilled之后,并不會取消其他promise對象的執行。 > 在?[ES6 Promises](http://liubin.github.io/promises-book/#es6-promises)?規范中,也沒有取消(中斷)promise對象執行的概念,我們必須要確保promise最終進入resolve or reject狀態之一。也就是說Promise并不適用于?[狀態](http://liubin.github.io/promises-book/#promise-states)?可能會固定不變的處理。也有一些類庫提供了對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>

                              哎呀哎呀视频在线观看