ES5 中 Promise.all 的意外解构

Unexpected destructuring with Promise.all in ES5

我正在使用 Promise.all 调用一组 Promise。我们的开发版本只支持 ES5。因此,当我使用以下语句时,ESLINT 会抛出错误:

Promise.all([
  service.document.getDocumentByPath( grantorPath ),
  service.document.getDocumentByPath( synonymPath ),
  service.document.getDocumentByPath( templatePath )
]).then(function([grantorDoc, synonymDoc, templateDoc]) {

ESLint error : Unexpected destructuring. eslint(es5/no-destructing)

我愿意

  1. 在不触及 eslint 规则的情况下删除 ESLINT 错误。
  2. 使用 Promise 解析后收到的结果(grantorDoc、synonymDoc、templateDoc)。

您的 ESLint 插件 forbids destructuring。由于听起来您的代码需要与 ES5 兼容,因此请改为在函数的第一行声明这些变量:

Promise.all([
  service.document.getDocumentByPath( grantorPath ),
  service.document.getDocumentByPath( synonymPath ),
  service.document.getDocumentByPath( templatePath )
]).then(function(result) {
  var grantorDoc = result[0];
  var synonymDoc = result[1];
  var templateDoc = result[2];
  // ...
});

(也就是说,如果你希望能够读写简洁可读的代码,用 ES6+ 编写并稍后使用 Babel 自动将代码转换为 ES5 可能更有意义)

确保您的环境支持 Promise.all,因为它是 ES6 功能 - 如果您还没有,请使用 polyfill。