使用自定义方法扩展$ .Deferred.pipe()?

时间:2012-09-17 20:40:49

标签: javascript jquery asynchronous

我想用自定义方法扩展$ .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()的问题。任何建议将不胜感激

2 个答案:

答案 0 :(得分:0)

为什么不简单地创建一些抽象管道逻辑的辅助方法呢?

function getFileEntry() {
   // Returns a promise resolved when your file entry is available
}

function getFileContents( entryPromise ) {
  return entryPromise.pipe(function( entry ) {
    return readFileEntry( entry );
  });
}

function readFileEntry( entry ) {
  var dfr = $.Deferred();

  // do whatever you have to do to read the file
  // then call dfr.resolve( fileContents );

  return dfr.promise();
}

function removeFileEntry( entry ) {
  var dfr = $.Deferred();

  // do whatever you have to do to remove the file
  // then call dfr.resolve();

  return dfr.promise();
}

使用这些方法,您的代码最终应如下所示:

var fileEntry = getFileEntry();

readFileEntry( fileEntry ).then(function(contents) {
    // Do something with your file contents
    removeFileEntry( fileEntry );
});

答案 1 :(得分:0)

我不认为自定义方法会有很多帮助,你仍然需要尊重链接(例如通过维护内部队列)。

在您的情况下,嵌套回调是最简单的事情:

getDeferredFileEntry().then(function(entry) {
    return entry.getDeferredText().then(function(text) {
    // after finished reading from it:
        console.log(txt); // do something with the read text
        console.log(fileEntry); // do something with the file entry
        return fileEntry.remove(); // e.g. deleting it
    });
});

如果您想要使用管道解决方案,可以将其简化为

.pipe(function(entry) {
    return entry.getDeferredText().pipe(function(fileText) {
        return {text:fileText, fileEntry:entry};
    });
})

...而不是手动构建Deferreds。