settimeout函数中的异步返回承诺

时间:2020-06-15 07:58:09

标签: javascript promise async-await

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()

预期输出:

hello2
1
hello4

输出为:

hello2 
undefined
hello4

如果我删除了settimeout函数并返回了这样的诺言

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

函数返回1并得到解决,当我使用settimeout函数返回时出现了什么问题

0 个答案:

没有答案