我想用自定義方法擴展$ .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
}
)
我與如何從getDeferredFileEntry()
解決FileEntry的對象傳遞給自定義read()
掙扎在背景中。任何意見,將不勝感激
getDeferredFileEntry是如何定義的?它所返回的promise對象將需要修改以添加額外的read方法,否則您將不得不將新的read方法添加到所有promise對象。 –