首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何通过回调函数使用异步等待

如何通过回调函数使用异步等待
EN

Stack Overflow用户
提问于 2019-04-11 00:23:19
回答 2查看 93关注 0票数 0

我正尝试从word文档中提取纯文本,使用word-extractor (https://www.npmjs.com/package/word-extractor)将其转换为csv。

不幸的是,csv是在从文档中提取数据之前创建的。我刚开始使用async/await,但这似乎是最好的选择。不幸的是,我正在努力将我的回调函数包装在一个promise中(我想)。

代码语言:javascript
复制
var WordExtractor = require("word-extractor");
var extractor = new WordExtractor();

let value = '';

// array of paths to files
const files = glob.sync('./desktop/docs/**/*.doc');

// loop through files   
for (let item of files) {

  // The object returned from the extract() method is a promise.
  let extracted = extractor.extract(item);

  // I need this part to finish before anything else happens. 
  function extractDocs() {
    return new Promise((resolve, reject) => {
      extracted.then(function(doc) {
        value = doc.getBody(); //takes around 1s
      });
    });
  }

// async await function 
async function test() {
return await extractDocs();
}

test() 

//rest of the code that writes a csv from the data extracted from docs 

对于这个措辞糟糕的问题,我们深表歉意,并对您的帮助表示感谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-11 01:39:58

由于word-extractor包已经支持promise,因此您可以执行以下操作:

代码语言:javascript
复制
// need async function to use await
async function extractDocs() {
  // loop through files
  // values = []; maybe store individual value if you plan to use it?
  for (let item of files) {

    // The object returned from the extract() method is a promise.
    // since it already returns a promise you can await
    // await will pause execution till the promise is resolved, sync like
    let extracted = await extractor.extract(item);
    const value = extracted.getBody();
    // now use value
    // values.push[value]
  }
  // return values so you can use it somewhere
  // return values
}
// execute

// extractDocs returns promise, so to use the return value you can do
async function useExtracted() {
  const values = await extractDocs(); // values is an array if you've returned
  //rest of the code that writes a csv from the data extracted from docs 
}
// execute
useExtracted()

async/await的一般语法是:

代码语言:javascript
复制
async function someThing() {
  try {
    const result = await getSomePromise() // if getSomePromise returns a promise
  } catch (e) {
    // handle error or rethrow
    console.error(e)
  }
}

注意:await仅在async函数内部有效,async函数返回的任何内容也都包装在Promise中。

票数 1
EN

Stack Overflow用户

发布于 2019-04-11 00:41:15

使用以下内容:

代码语言:javascript
复制
async function extractDocs() {
  let promise = new Promise((resolve, reject) => {
    extracted.then(function(doc) {
      value = doc.getBody(); //takes around 1s
    });
  });
  let result = await promise; // wait till the promise
  return result
}

要在代码之后运行,请使用.then()

代码语言:javascript
复制
extractDocs()
  .then(console.log)
  .catch(console.error)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55617247

复制
相关文章

相似问题

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