【发布时间】:2012-09-10 02:42:45
【问题描述】:
我想用自定义方法扩展 $.Deferred.pipe,这样我就可以稍微缩短我的延迟链。
我目前的代码是这样的
getDeferredFileEntry()
//returns a ($.Deferred) promise to resolve with a FileEntry object
.pipe(function(entry){
//returns a promise to resolve with an object
//containing the contents of the file as text
//and a reference to the file's FileEntry
var def = $.Deferred();
entry.getDeferredText()
.done(function(fileText){
def.resolve({text:fileText, fileEntry:entry});
});
return def.promise();
)}
.done(function(response){
var text = response.text;
var fileEntry = response.fileEntry;
console.log(text);
//do something with the text
console.log(fileEntry);
//do something else with the file entry after finished reading from it
//e.g. deleting with something like fileEntry.remove();
});
我想把它缩短为
getDeferredFileEntry()
.read(
//uses the FileEntry object resolved by getDeferredFileEntry
//to call an asynchronous fileEntry.read() *in the background*
//the contents are then passed to the callback taken from below
//returns promise to resolve with the fileEntry object for chaining
function callback(text){
//do something with the text
console.log(text);
}
)
.remove(
function(fileEntry){
//library call to remove fileEntry that read() promised
}
)
我正在努力解决如何将 FileEntry 对象从 getDeferredFileEntry() 解析到后台的自定义 read()。任何建议将不胜感激
【问题讨论】:
-
getDeferredFileEntry 是如何定义的?它返回的 promise 对象需要修改以添加额外的 read 方法,或者您必须将新的 read 方法添加到所有 promise 对象。
标签: javascript jquery asynchronous