SOURCE

function getData() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve("data")
        }, 1000)
    })
}

async function test() {
    let data1 = await getData()
    console.log('data1', data1)
    let data2 = await getData()
    console.log('data2', data2)
    return 'success'
}

// test().then(data => {
//     console.log(data)
// })

function* genTest() {
        let data1 = yield getData()
        console.log('data1', data1)
        let data2 = yield getData()
        console.log('data2', data2)
        return 'success'
    }

function autoGen(genFn) {

    let gen = genFn.apply(this)

    function next(value, resolve, reject) {
        let res = gen.next(value)
        if (res.done) {
            resolve(res.value)
        } else {
            res.value = Promise.resolve(res.value)
            res.value.then(data => {
                next(data, resolve, reject)
            })
        }
    }

    return new Promise((resolve, reject) => {
        next(null, resolve, reject)
    })
}

autoGen(genTest).then(data => {
    console.log(data)
})
console 命令行工具 X clear

                    
>
console