const promiseFn = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(100);
}, 2000);
});
};
const foo = async () => {
let x = "";
if (x) {
console.log("2");
} else {
const data = await promiseFn();
x = data;
console.log("3");
}
console.log("4");
const user = {
userId: x,
};
};
foo();
打印结果为3,4。 问题为:await是异步的,为何打印结果不是4,3?
await 会暂停当前函数执行,直到 promise resolve 。