javascript - 如何从 jQuery 延迟传递参数

标签 javascript jquery backbone.js jquery-deferred promise

我有一个 Backbone 模型,可以获取一些数据,处理数据,然后函数应该获取处理后的数据。

$.when(model.fetch())
  .done(function(){
    return model.processData()
  })
  .then(function(processedData){
    //make something with the processed data
  })

不幸的是then方法从 model.fetch() 获取结果调用而不是 done 的返回值功能

最佳答案

您必须使用.then而不是.done.then 返回一个新的 Promise,该 Promise 使用回调函数返回的值进行解析。

另一方面,

.done 返回原始的 Promise 对象,并且回调的返回值将被忽略。

更多信息可以在documentation中找到(强调我的):

As of jQuery 1.8, the deferred.then() method returns a new promise that can filter the status and values of a deferred through a function, replacing the now-deprecated deferred.pipe() method. The doneFilter and failFilter functions filter the original deferred's resolved / rejected status and values. The progressFilter function filters any calls to the original deferred's notify or notifyWith methods. These filter functions can return a new value to be passed along to the promise's .done() or .fail() callbacks, or they can return another observable object (Deferred, Promise, etc) which will pass its resolved / rejected status and values to the promise's callbacks. If the filter function used is null, or not specified, the promise will be resolved or rejected with the same values as the original.

关于javascript - 如何从 jQuery 延迟传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17083532/

相关文章:

javascript - firebase实时数据库中的重复数据(javascript)

javascript - 在 cshtml/razor 中使用 <script src ="@microsoft/"问题

javascript - 在提交后修改表单数据?

javascript - 主干 - 模型中的对象数组?

javascript - 类似 Greasemonkey 的 Firefox 插件,用于自动浏览

jquery - 如何删除浏览器上的空TD

javascript - jQuery 插件样板 - 具有绑定(bind)范围的私有(private)方法?

javascript - jQuery $(this) 重复

javascript - 将默认值设置为模型中的局部变量会导致未定义

javascript - 使用 Backbone 样式的原型(prototype)继承时防止无限递归