小编prs*_*s99的帖子

如何在不创建 Promise 的情况下将 await 与异步函数一起使用?

我知道一个异步函数返回一个 Promise,所以我想到了替换这段代码:

const hi = function (delay) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                console.log('hi');
                resolve();
            },delay)
        });
    };

const bye = async () => {
    await hi(1000);
    await hi(1000);
    await hi(1000);
    await hi(1000);
    return 'bye';
};

bye().then((msg) => {
    console.log(msg);
});
Run Code Online (Sandbox Code Playgroud)

使用此代码:

const hi = async (delay) => {
    setTimeout(() => {
        console.log('hi');
    },delay);
};

const bye = async () => {
    await hi(1000);
    await hi(1000);
    await hi(1000);
    await hi(1000);
    return 'bye';
};

bye().then((msg) => { …
Run Code Online (Sandbox Code Playgroud)

javascript async-await

1
推荐指数
1
解决办法
65
查看次数

标签 统计

async-await ×1

javascript ×1