mirror of
				https://github.com/KieSun/all-of-frontend.git
				synced 2025-05-29 01:49:23 +08:00 
			
		
		
		
	
		
			
				
	
	
	
		
			876 B
		
	
	
	
	
	
	
	
			
		
		
	
	
			876 B
		
	
	
	
	
	
	
	
try {
    let a = 0
    ;(async function() {
        a += 1
        console.log('inner', a)
        throw new Error('123')
        // a()
    })()
    console.log('outer', a)
} catch(e) {
    console.warn('Error', e)
}
这道题目基础不好的同学容易答错,很容易以为 async 的函数内部代码就是异步的,实际都还是同步的,把代码改成这样大家就明白了:
function a() {
  return new Promise(function(resolve, reject) {
    a += 1
    console.log('inner', a)
    throw new Error('123')
  })
}
另外发现还有同学疑问为什么 throw 出去的错误被 promise catch 住了,这个我们直接看标准: