<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 第一種 ``` const fs = require('fs') const path = require('path') function getFileByPath(fpath) { return promise = new Promise(function (resolve, reject) { fs.readFile(path.join(__dirname, fpath), 'utf-8', (err, data) => { if (err) return reject(err); resolve(data); }); }); } //如果前面的Promise執行失敗,我們不想讓后續的Promise操作被終止,可以為每個Promise指定失敗的回調 getFileByPath('./1.txt') .then(function (data) { console.log(data); return getFileByPath('./2.txt'); }, function (error) { //return 一個新的Promise,讓后續的可以繼續執行 console.log('文件1失敗:' + error.message) return getFileByPath('./2.txt'); }).then(function (data) { console.log(data); return getFileByPath('./3.txt'); }, function (error) { console.log('文件2失敗:' + error.message) return getFileByPath('./3.txt'); }).then(function (data) { console.log(data) }, function (error) { console.log('文件3失敗:' + error.message) }).catch(function (error) { console.log(error.message); }) console.log('ok') ``` 結果 ``` ok //因為promise是異步操作 111 222 333 ``` 哪怕前面的promise執行失敗了,但是不要影響后序的promise的正常執行,此時,我們可以單獨為每個promise通過.then指定一下失敗的回調. # 第二種 有時候我們有這樣的需求,和上面的需求剛好相反,如果后序的promise執行依賴于前面的promise執行的結果,如果前面的失敗了,則后面的就沒有繼續執行下去的意義了,此時,我們想要實現,一旦有報錯,則離職終止所有promise的執行. ``` const fs = require('fs') const path = require('path') function getFileByPath(fpath) { return promise = new Promise(function (resolve, reject) { fs.readFile(path.join(__dirname, fpath), 'utf-8', (err, data) => { if (err) return reject(err); resolve(data); }); }); } //如果前面的Promise執行失敗,我們不想讓后續的Promise操作被終止,可以為每個Promise指定失敗的回調 getFileByPath('./1.txt') .then(function (data) { console.log(data); return getFileByPath('./12.txt'); }) .then(function (data) { console.log(data); return getFileByPath('./3.txt'); }) .then(function (data) { console.log(data) }) .catch(function (error) { //catch的作用:如果前面有任何的promise執行失敗,則立即終止所有的promise的執行,并馬上進行catch去處理promise中拋出的異常 console.log(error.message); }) console.log('ok') ```
                  <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>

                              哎呀哎呀视频在线观看