`env` (preset-env style) targets don't lower `for await` inside a plain `async function`, unlike `jsc.target`
Describe the bug
(investigated with the help of Claude, I might have missed something)
When compiling for a target that doesn't natively support async iteration (e.g. Chrome 60, which supports async/await but not for await...of — that shipped in Chrome 63), SWC correctly lowers an async function* generator to the _async_generator/_wrap_async_generator runtime helpers. However, a for await (...) loop inside an ordinary async function (not a generator) is left completely untouched, and is emitted verbatim in the output — but only when using the env config (browserslist-driven, per-feature detection, e.g. mode: "usage"/"entry").
Using the simpler jsc.target: "es5" config on the exact same input correctly lowers both cases via the _ts_generator/_async_iterator machinery. So this is specifically a gap in the env/preset-env-style compilation path, not the jsc.target path — the two paths clearly use different lowering logic for for-await-of, and only one of them handles the "for-await inside an ordinary async function" case.
This looks like the residual gap called out in review on #12086 (which fixed #11958's Chrome-55-vs-63 threshold for async function* declarations): "Lower for-await in async functions for async-iteration targets — the pass skips ordinary async function bodies, leaving unsupported for await syntax untransformed." That comment was never turned into its own tracked issue, so filing this to track it explicitly.
Input code
async function getAll(iterable) {
const results = [];
for await (const item of iterable) {
results.push(item);
}
return results;
}
async function* gen(iterable) {
for (const item of iterable) {
yield item;
}
}Config
{
jsc: {
parser: { syntax: 'ecmascript' },
},
env: {
targets: { chrome: '60' },
mode: 'usage',
},
}Link to the code that reproduces this issue
SWC Info output
Operating System:
Platform: darwin
Arch: arm64
Machine Type: arm64
Version: Darwin Kernel Version 25.6.0: Fri Jul 31 19:19:08 PDT 2026; root:xnu-12377.161.14~5/RELEASE_ARM64_T6050
CPU: (18 cores)
Models: Apple M5 Pro
Binaries:
Node: 24.18.1
npm: 11.16.0
Yarn: N/A
pnpm: 11.1.2
Relevant Packages:
@swc/core: 1.16.2
@swc/helpers: 0.5.23
@swc/types: N/A
typescript: 6.0.2
SWC Config:
output: N/A
.swcrc path: N/A
Next.js info:
output: N/AExpected behavior
getAll's for await loop should be lowered the same way under the env config as it already is under jsc.target, when the resolved target doesn't support native async iteration.
function getAll(iterable) {
return _async_to_generator(function() {
var results, _iteratorAbruptCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, item, err;
return _ts_generator(this, function(_state) {
// ... correctly drives the async iterator via _iterator.next() ...
});
})();
}Actual behavior
async function getAll(iterable) {
const results = [];
for await (const item of iterable){ // <-- NOT lowered, ships as-is
results.push(item);
}
return results;
}Version
1.16.2
Additional context
Found via a production bundle (Rspack + swc-loader, using the env config with a browserslist target equivalent to Chrome ≥71/Firefox ≥64/Safari ≥12) that runs es-check --checkFeatures against the compiled output. A third-party dependency (@uppy/utils, getDroppedFiles/utils/webkitGetAsEntryApi) uses for await inside plain async functions (not generators) for directory-drag-and-drop handling. In the same file, async function*/async * declarations, optional chaining, nullish coalescing, and logical assignment were all correctly lowered/removed by the env config — only the for await loop statements survived untransformed.
Source: swc-project/swc