Skip to content

ES5 async array destructuring awaited value replaced with pending Promise #11049

@Yeaseen

Description

@Yeaseen

Describe the bug

When using nested array destructuring with holes inside an async function, SWC’s ES5 output mishandles the destructuring. The transform drops the hole and shifts values, causing one of the awaited results to be replaced with a raw, unresolved Promise. Node.js and Babel transpilation both yield the correct values.

Input code

class MyClass {
    constructor(data) {
        if (!(data instanceof Array))
            throw new Error('Invalid instance');
        this.data = data;
    }
}

function fetchPromise(idx) {
    return new Promise((resolve, reject) => {
        setTimeout(() => resolve(idx), 10)
    })
}

async function func1() {
    const arr = [1, [2, 3], , 4];
    let [a, [ , b], ,d] = arr;

    const instance = new MyClass([a, b, d])

    a = await fetchPromise(instance.data[0]) || fetchPromise(0);
    b = await fetchPromise(instance.data[1]) || fetchPromise(0);
    d = await fetchPromise(instance.data[2]) || fetchPromise(0);
    
    return [a, b, d];
}

function main() {
    const res = func1();
    res.then(value => console.log(value));
}

main();

Config

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.13.5&code=H4sIAAAAAAAAA41SsW7CMBDd8xXXCVuyEKHdIipVVYcOlTp0izIY51JcBVuyHSgq%2FHvPToDAgLjF0b137%2B7eRbXSe%2FjYvab3LwMKZY0PrlPBOlbLIPmQj6EbYA8pC5pY0ii0Dbw4J3ecn1gxwsrZLRjcwptzpDR5NxvZ6vpUN%2BFFdiZrP02qC4hPjxyyQ5Y1nVFBWwMNBrX6dHatPTJd%2Fx7Hchg6Z1KnI8ocettuUBD4gypwWDyPlvAYvvQabRcYS9BAT6oC8lm%2FyYHH%2FtLvjILzFPSRMz62CqRzNHeZCyjnAh4rAQKeqn6HFgOUkhDKLSNSV8SliiIbKRw9ISjuMdyDxcKlgLriPTnaI7dShyszhurkYDmrOOz3l4zZ4PXyHoH8hkB9j8D8hsD4ZKf1iotDr6U2Vw7Tgajx4H0xaPhpWKFh9Fd1GK8YqbbFaWu%2F%2ByTnSbjXK%2F4BwB%2BqNusCAAA%3D&config=H4sIAAAAAAAAA1WPSQ6DMAxF95wi8pptu%2BgdOIQVDArKpDiVGiHu3pCBll38%2F7Ofsg9CwMYSXmLPzzx4DEzhmnPCyUb85ARIGmQZlI8w9nbjs1pQM5XoqA1EDCvFssWPhoN2jqnjLTPKqiX9C6UzPhDzHTxRtKumu25oSjBufpeyfSQmT1X%2FhB%2FUZddhUDz1zXr2%2BAIDhbm2FQEAAA%3D%3D

SWC Info output

No response

Expected behavior

SWC's ES5 output should print: [ 1, 3, 4 ]

Actual behavior

But, it prints: [ 1, 3, Promise { <pending> } ]

Version

1.13.5

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions