我想用自定义方法扩展 $.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()
。任何意见,将不胜感激