我有一个 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-deprecateddeferred.pipe()
method. ThedoneFilter
andfailFilter
functions filter the original deferred's resolved / rejected status and values. TheprogressFilter
function filters any calls to the original deferred'snotify
ornotifyWith
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 isnull
, 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/