用async匿名函数时候会出现的问题
为了在最外层尽快能用await, 就需要用到async匿名函数, 一般这么写:
(async ()=>{
xxx = await xxxxx();
})();
这么一般很正常, 直到这段代码前面有个{}
const api = {}
(async ()=>{
xxx = await xxxxx();
})();
看起来很正常不是吗, 然而会报错:
(async () => {
^
TypeError: {(intermediate value)} is not a function
什么鬼? 这是因为本来要正常在每行后加的;这次没有加上!因为有歧义!
const api={}被系统看成了const api={}(), 注意{}后面带有()表达了定义一个匿名函数再运行的意思, 所以上面代码要手动加上;改为:
const api = {}; //这儿加上分号
(async ()=>{
xxx = await xxxxx();
})();