-2

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.
VLAZ
  • 26,331
  • 9
  • 49
  • 67
Rahul Bhardwaj
  • 2,303
  • 1
  • 17
  • 28
  • What about disabling the control on the next line? https://stackoverflow.com/questions/27732209/turning-off-eslint-rule-for-a-specific-line – briosheje May 13 '19 at 07:03
  • 1
    So did you try writing the code to *not* use destructuring? You can remove the error by not writing code that's invalid in ES5. – jonrsharpe May 13 '19 at 07:04
  • As I mentioned in the question, I do not want to touch the ESLINT rules. – Rahul Bhardwaj May 13 '19 at 07:04
  • 1
    Why can't you use a code that doesn't use destructuring? – Rajaprabhu Aravindasamy May 13 '19 at 07:04
  • 1
    object destructuring is something came with es6, so if you don't want to change eslint rules follow them. – AZ_ May 13 '19 at 07:07
  • @RahulBhardwaj using ignore on the next line you **don't** touch eslint rules. You just add a rule for a single line specifically which is about ignoring just that line, you don't alter the entire eslint rules. Otherwise, you may just want to don't use destructuring, since it's an ES6 feature and you're using eslint targeting ES5 support. – briosheje May 13 '19 at 07:10

1 Answers1

0

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.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • As an aside, Promises came into JS in ES6. It's possible this is actually using Bluebird or another alternative, but OP might want to look into this more. If this *is* an ES6 promise, then it probably shouldn't be. Or it might signal that ES6 code is OK. – VLAZ May 13 '19 at 07:17