如何从异步自执行函数的内侧设置窗口变量。当我在 fetch 之前使用 await 时,我得到“错误:未捕获的 ReferenceError:bar 未定义”。当我删除等待时,在这种情况下,我得到了 Promise 对象。
(async function (w){
bar = await fetch('https://jsonplaceholder.typicode.com/posts').then(()=>{
response.json()
}).then((data)=>{
return data
});
w.bar=bar;
})(window);
console.log(bar);