[Javascript] Promise question with async await

Zhentiw發表於2024-10-06
async function asy1() {
    console.log(1)
    await asy2()
    console.log(2)
}

asy2 = async () => {
    // First set
    // await setTimeout((_) => {
    //     Promise.resolve().then(() => {
    //         console.log(3)
    //     });
    //     console.log(4)
    // }, 0)
    // 1 7 6 2 4 3

    // Second set
    // await (async () => {
    //     await (() => {
    //         console.log(3)
    //     })()
    //     console.log(4)
    // })()
    // 1 3 7 4 6 2

    // Third set
    // await (async () => {
    //     Promise.resolve().then(() => {
    //         console.log(3)
    //     });
    //     console.log(4);
    // })()
    // 1 4 7 3 6 2

    // Fourth set
    await Promise.resolve().then(() => {
        Promise.resolve().then(() => {
            console.log(3)
        });
        console.log(4)
    })
    console.log(5)
    // 1 7 4 6 3 5 2
}

asy3 = async () => {
    Promise.resolve().then(() => {
        console.log(6)
    })
}
asy1()
console.log(7)
asy3()

相關文章