【问题标题】:Using jQuery.Deferred to avoid nested setTimeout callbacks使用 jQuery.Deferred 避免嵌套的 setTimeout 回调
【发布时间】:2013-08-01 17:49:56
【问题描述】:
setTimeout ->
  console.log 'foo'
  setTimeout ->
    console.log 'bar'
    setTimeout ->
      console.log 'baz'
    , 1000
  , 1000
, 1000

是否可以使用 jQuery.Deferred 实现相同的结果?可能类似于以下内容:

someFunction()
.then(-> console.log 'foo')
.then(delay 1000)
.then(-> console.log 'bar')
.then(delay 1000)
.then(-> console.log 'baz')

也许我错误地认为 promise 让编写变得容易:做 A,完成后做 B,完成后做 C

【问题讨论】:

标签: jquery coffeescript promise deferred


【解决方案1】:

您可以通过返回 new Deferred 对象来链接 .then() 调用。特别是为了延迟,你可以使用类似的东西:

function someFunction() {
    var ret = new $.Deferred();
    // setTimeout just to simulate `someFunction` taking 1000ms to complete its deferred
    setTimeout(function () {
        ret.resolve();
    }, 1000);
    return ret;
}

function logger(str) {
    return function () {
        console.log("Logger:", str);
    };
}

function delay(time) {
    return function () {
        console.log("Delaying");
        var ret = new $.Deferred();
        setTimeout(function () {
            ret.resolve();
        }, time);
        return ret;
    };
}

someFunction()
    .then(logger("foo"))
    .then(delay(3000))
    .then(logger("bar"))
    .then(delay(3000))
    .then(logger("baz"));

演示: http://jsfiddle.net/yGcfu/

【讨论】:

    【解决方案2】:

    是的:

    delay = (ms) -> ->
        d = $.Deferred()
        setTimeout d.resolve, ms
        d.promise()
    

    (demo at jsfiddle.net)

    或者,对于delay 方法更常见的是,您省略闭包(一个->)并使用

    ….then(-> delay 1000) …
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-17
      • 1970-01-01
      • 2019-05-18
      • 2010-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多