Javascript: Asynchronous iteration (for-await-of) and ForOfStatement deprecation

Created on 4 Mar 2018  路  3Comments  路  Source: airbnb/javascript

'use strict';

(async () => {
  for await (const item of [Promise.resolve('a'), Promise.resolve('b')]) {
    console.log(item);
  }
})();
  4:3  error  iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations  no-restricted-syntax

Should the ForOfStatement entry in no-restricted-syntax be alleviated in such cases? Maybe it is worth to remove the second part of the message ("loops should be avoided in favor of array iterations"), since there seems to be no easy different way for array asynchronous iteration?

Is there a way for AST parser to distinguish an async ForOfStatement from a sync one?

question

Most helpful comment

@vsemozhetbyt if you're in a node-only environment, you can certainly override rules that are concerned with transpilation overhead.

All 3 comments

In this case, you're iterating an array of promises you already have, so using async iteration provides precisely zero benefit.

[Promise.resolve('a'), Promise.resolve('b')].forEach((promise) => {
  promise.then((item) => { console.log(item); });
});

Regardless, no, it should be alleviated in no cases, because the transpilation of it still currently requires regenerator-runtime.

@vsemozhetbyt if you're in a node-only environment, you can certainly override rules that are concerned with transpilation overhead.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brendanvinson picture brendanvinson  路  4Comments

graingert picture graingert  路  3Comments

stephenkingsley picture stephenkingsley  路  3Comments

ehrudxo picture ehrudxo  路  4Comments

surfaceowl picture surfaceowl  路  3Comments