Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

关于async里面try/catch的一个问题

$
0
0
function foo(){ throw new Error('zjl')}
function*fn(){
	try{
		yield foo();
	}catch(e){
		console.log(e)
	}
}

上面会被caty/catch ,而下面这个Promise就不会被try/catch

function * fn(){
  yield Promise.reject('ixl')
}
//执行next后,会报错.
function *fn(){
  try{
     yield Promise.reject('ixl')
  }catch(e){
    console.log(e)
  }
}
//也会报错

而async里面,我看了下对await进行try/catch 可以避免 处于rejected状态的promise跳出async函数 怎么做到的?


Viewing all articles
Browse latest Browse all 14821

Trending Articles