首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于ts/js-library promise

基于ts/js-library promise
EN

Stack Overflow用户
提问于 2018-01-14 06:42:31
回答 1查看 31关注 0票数 0

我已经在我的项目中实现了this file reader

我想让这个返回成为一个承诺,当它完成文件读取时,但我不知道如何从那里传播承诺。

代码语言:javascript
复制
class MyClass {

  constructor() {}

  public start(file) {
    parseFile(file);
  }

  private parseFile(file) {
      let fileSize = file.size;
      let chunkSize = 10000;
      let offset = 0;
      let self = this;
      let readBlock = null;
      // How do I get this success function to return a promise to the user?
      let success = function() { return new Promise...? };

      let onLoadHandler = function(evt) {
          if (evt.target.error == null) {
              offset += evt.target.result.length;
              chunkReadCallback(evt.target.result);
          } else {
              chunkErrorCallback(evt.target.error);
              return;
          }
          if (offset >= fileSize) {
              success(file);
              return;
          }

          readBlock(offset, chunkSize, file);
      }

      readBlock = function(_offset, length, _file) {
          let r = new FileReader();
          let blob = _file.slice(_offset, length + _offset);
          r.onload = onLoadHandler;
          r.readAsText(blob);
      }

      readBlock(offset, chunkSize, file);
   }
}

今天它是这样工作的:

代码语言:javascript
复制
let x = new MyClass();
x.start(file);

我希望它是这样的:

代码语言:javascript
复制
let x = new MyClass();
x.start(file).then(() => { console.log('done') });

我将我的返回承诺放在哪里,以便用户可以处理该承诺?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-01-14 10:28:56

下面的代码将把readFile变成一个promise:

代码语言:javascript
复制
private parseFile(file,chunkSize,offset) {
  let fileSize = file.size;
  let self = this;

  readBlock = function (_offset, length, _file) {
    return new Promise(
      function(resolve,reject){
        let r = new FileReader();
        let blob = _file.slice(_offset, length + _offset);
        //https://developer.mozilla.org/en-US/docs/Web/API/FileReader/onload
        r.onload = function(e){
          if(e.target.error!==null){
            reject(e.target.error);
          }
          else{
            resolve(e.target.result)
          }
        };
        //https://developer.mozilla.org/en-US/docs/Web/API/FileReader/onerror
        r.onerror = function(err){
          reject(err);
        }
        r.readAsText(blob);    
      }
    )
  }

  return readBlock(offset, chunkSize, file);
}

您可以让调用者定义块的大小以及何时读取下一个块。

此函数的使用示例如下:

代码语言:javascript
复制
x.parseFile(file,file.size,0)
.then(
  function(textData){
    console.log(textData);
  }
);

//read in chunks of 1000
function readInChunks(file,chunkSize=1000,offset=0,data=""){
  return x.parseFile(file,chunkSize,offset)
  .then(
    function(textData){
      if(offset+chunkSize>=file.size){
        return data+textData;
      }
      console.log("get next chunk");
      //recursively call itself
      return readInChunks(file,chunkSize,offset+chunkSize,data+textData);
    }
  )
}
//call read in chunks
readInChunks(file,/* optional, defaults to 1000 */500)
.then(
  function(textData){
    console.log("got data:",textData);
  }
) 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48244972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档