如何使Polymer 2.x函数异步

问题描述 投票:0回答:1

我正在尝试使用形状检测API(https://developers.google.com/web/updates/2019/01/shape-detection)并收到错误:

未捕获的SyntaxError:await仅在异步函数中有效

通过Polymer 2.x docs(https://polymer-library.polymer-project.org/2.0/api/namespaces/Polymer.Async)后,我得到以下内容:

ready() {
  super.ready();
  this.initImageDetection();
}

initImageDetection() {
  const barcodeDetector = new BarcodeDetector({
    formats: [
      'code_128'
    ]
  });
  try {
    const barcodes = await barcodeDetector.detect(image);
    barcodes.forEach(barcode => console.log(barcode));
  } catch (e) {
    console.error('Barcode detection failed:', e);
  }
}

此模式也失败并出现相同的错误:

this.async(() => {
  const barcodes = await barcodeDetector.detect(image)
  barcodes.forEach(barcode => console.log(barcode)
)});

此外,运行initImageDetection前缀为async并在加载DOM后从paper-button运行。

async initImageDetection() {
  ...
}

我收到以下错误:

未捕获(在承诺中)ReferenceError:未定义BarcodeDetector

如何在Polymer 2.x中正确生成异步函数?

如何在Polymer 2.x中实例化BarcodeDetector?

asynchronous async-await polymer-2.x
1个回答
0
投票
async functionName() {
  // function code here
}

是在Polymer中设置异步函数的正确方法。但是,BarcodeDetector对象隐藏在Chrome中的标志后面,因此必须在使用之前在chrome://flags Experimental Web Platform features中启用。

© www.soinside.com 2019 - 2024. All rights reserved.