async is a promise function and with setimeout how it works [duplicate]

江枫思渺然 提交于 2020-07-03 12:56:31

问题


const name = async () => {
       setTimeout(() => {
        return Promise.resolve("1")
       },1000);
    };

    (async function () {
        console.log("hello2");
    })();

    const timer = async () => {
        console.log(await name());
        console.log("hello4");
    };
    timer()

    expected output 
    hello2
    1
    hello4

    output is
    hello2 
    undefined
    hello4

If i removed the settimeout function and return a promise alobe like this

const name = async () => {
        return Promise.resolve("1")
    };

function is returning 1 and it is getting resolved, what is the issue with when i am returning with settimeout function

来源:https://stackoverflow.com/questions/62381734/async-is-a-promise-function-and-with-setimeout-how-it-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!