【问题标题】:jQuery Deferred - catch vs failjQuery Deferred - 捕获与失败
【发布时间】:2016-03-22 23:44:53
【问题描述】:

我想确保我没有错过任何技巧;在 Kris Kowal 的库中,您可以在 Promise 中作为通用 catch 语句执行以下操作:

var a, b, c, d, e, f;

readFile('fileA')
    .then(function (res) {
        a = res;

        return readFile('fileB');
    })
    .then(function (res) {
        b = res;

        return readFile('fileC');
    })
    .then(function (res) {
        c = res;

        return readFile('fileD');
    })
    .then(function (res) {
        d = res;

        return readFile('fileE');
    })
    .then(function (res) {
        e = res;

        return readFile('fileF');
    })
    .then(function () {
        f = res;
    })
    .catch(function () {
        // error happened in file read *somewhere* (don't care where)
    });

在 jQuery 的延迟对象中,没有catch 语句,相反,我必须这样做:

var a, b, c, d, e, f;

readFile('fileA')
    .then(function (res) {
        a = res;

        return readFile('fileB');
    })
    .fail(function () {
        // error happened in file read *somewhere* (don't care where)
    })
    .then(function (res) {
        b = res;

        return readFile('fileC');
    })
    .fail(function () {
        // error happened in file read *somewhere* (don't care where)
    })
    .then(function (res) {
        c = res;

        return readFile('fileD');
    })
    .fail(function () {
        // error happened in file read *somewhere* (don't care where)
    })
    .then(function (res) {
        d = res;

        return readFile('fileE');
    })
    .fail(function () {
        // error happened in file read *somewhere* (don't care where)
    })
    .then(function (res) {
        e = res;

        return readFile('fileF');
    })
    .fail(function () {
        // error happened in file read *somewhere* (don't care where)
    })
    .then(function (res) {
        f = res;

        return readFile('fileF');
    })
    .fail(function () {
        // error happened in file read *somewhere* (don't care where)
    });

不幸的是,每个then 分支都有独特的逻辑。我是否遗漏了什么,或者上面的 jQuery 变体是在 Kris Kowal 的 q 库中实现等效的唯一方法?

【问题讨论】:

  • 您是否尝试链接 then() 并在最后使用单个 .fail();?它应该可以工作。
  • 这是 Sebastien 的典型问题,我仍然不确定是否可以将 catch 替换为失败
  • @A.Wolff 啊.. 谁塞巴斯蒂安..?另一只狼..? ._.
  • @keldar,部分问题是 jQuery 在 Promises/A+ 标准化之前添加了 Deferreds(我的理解是,大部分标准化是在开发人员学习使用类似 Promise 的行为之后进行的jQuery 的延迟)。幸运的是jQuery 3 will be Promises/A+ compatible
  • 简而言之,jQuery 的.fail() 不能捕捉也不能被捕捉。在fail() 之后,链保证保持在其错误路径上。另一方面,.then() 的错误处理程序不会自动捕获,但可以通过返回已解决的承诺来实现;返回除了已解决的承诺之外的任何内容都会改变沿错误路径传播的“失败原因”。

标签: javascript jquery promise jquery-deferred deferred


【解决方案1】:

假设 readFile 返回一个 promise 对象,您实际上可以使用 $.when() 异步加载所有文件(当然,如果您不关心文件读取的顺序):

来自文档:

在将多个 Deferred 对象传递给 jQuery.when() 的情况下,该方法从一个新的“主” Deferred 对象返回 Promise,该对象跟踪它已传递的所有 Deferred 的聚合状态。该方法将在所有 Deferred 解决后立即解决其 master Deferred,或者在其中一个 Deferred 被拒绝后立即拒绝 master Deferred。如果 master Deferred 已解决,则执行 master Deferred 的 doneCallbacks。传递给 doneCallbacks 的参数为每个 Deferred 提供解析值,并匹配 Deferred 传递给 jQuery.when() 的顺序

强调我的

$.when(readFile('fileA'), readFile('fileB'), readFile('fileC'), readFile('fileD'), readFile('fileE'), readFile('fileF'))
.then(function(a, b, c, d, e, f) {
    // big success
},function() {
    // error happened in file read *somewhere* (don't care where)
});

【讨论】:

  • 并行运行它们,而不是按顺序运行
  • 看看他们的文档,我当然可以$.when(readFile('fileA'), readFile('fileB'), ..., readFile('fileF')).then(function (a, b, ..., f) {...}); a, b, ..., f 是文件的内容吗?
  • @keldar 是的,为了清楚起见更新了答案。请注意,您的 readFile 方法应该返回一个延迟对象。
  • 谢谢@TJ。同意 - 同意(或公平的延期承诺)。
猜你喜欢
  • 1970-01-01
  • 2011-09-09
  • 2016-02-16
  • 2017-08-06
  • 2013-03-24
  • 2015-02-08
  • 2016-08-02
  • 2014-01-24
  • 1970-01-01
相关资源
最近更新 更多