Search code examples
javascriptecmascript-6es6-promiseeslintecmascript-5

Unexpected destructuring with Promise.all in ES5


I'm using Promise.all to call a set of Promises. Our development version only supports ES5. Hence, the ESLINT throws an error when I use the following statement :

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)

I would like to

  1. remove the ESLINT error without touching the eslint rules.
  2. use the results (grantorDoc, synonymDoc, templateDoc) recevied after the Promises are resolved.

Solution

  • Your ESLint plugin forbids destructuring. Since it sounds like your code needs to be ES5 compatible, declare those variables on the first lines of the function instead:

    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];
      // ...
    });
    

    (that said, it would probably make more sense to write in ES6+ and automatically transpile the code to ES5 later with Babel, if you want to be able to read and write concise readable code)

    Make sure that your environment supports Promise.all, since it's an ES6 feature - use a polyfill, if you aren't already.