jQuery延迟了多个处理程序

时间:2018-03-31 18:32:49

标签: javascript jquery

根据jQuery文档(https://api.jquery.com/deferred.then/),应该可以将多个成功或失败回调作为函数数组传递给.then()。但是,它似乎没有按照规定运行。

它似乎与.done()和.fail()一样正常运行。

这是一个jQuery错误,还是我错过了什么?

$(() => {
  $.Deferred().resolve('Fulfilled')
    .then([
        (m) => {
            console.log(m, 'Then Fulfilled 1')
            return $.Deferred().resolve(m)
          },
        (m) => {
            console.log(m, 'Then Fulfilled 2')
            return $.Deferred().resolve(m)
          }
      ], [
        (m) => {
            console.log(m, 'Then Rejected 1')
            return $.Deferred().reject(m)
          },
        (m) => {
            console.log(m, 'Then Rejected 2')
            return $.Deferred().reject(m)
          }
      ])
    .done([
        (m) => {
            console.log(m, 'Done 1')
            return $.Deferred().resolve(m)
          },
        (m) => {
            console.log(m, 'Done 2')
            return $.Deferred().resolve(m)
          }
      ])
    .fail([
        (m) => {
            console.log(m, 'Fail 1')
            return $.Deferred().reject(m)
          },
        (m) => {
            console.log(m, 'Fail 2')
            return $.Deferred().reject(m)
          }
    ])
  $.Deferred().reject('Rejected')
    .then([
        (m) => {
            console.log(m, 'Then Fulfilled 1')
            return $.Deferred().resolve(m)
          },
        (m) => {
            console.log(m, 'Then Fulfilled 2')
            return $.Deferred().resolve(m)
          }
      ], [
        (m) => {
            console.log(m, 'Then Rejected 1')
            return $.Deferred().reject(m)
          },
        (m) => {
            console.log(m, 'Then Rejected 2')
            return $.Deferred().reject(m)
          }
      ])
    .done([
        (m) => {
            console.log(m, 'Done 1')
            return $.Deferred().resolve(m)
          },
        (m) => {
            console.log(m, 'Done 2')
            return $.Deferred().resolve(m)
          }
      ])
    .fail([
        (m) => {
            console.log(m, 'Fail 1')
            return $.Deferred().reject(m)
          },
        (m) => {
            console.log(m, 'Fail 2')
            return $.Deferred().reject(m)
          }
    ])
})
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

0 个答案:

没有答案