使用生成器和承诺时传播异常

Mer*_*lli 5 javascript exception-handling generator promise ecmascript-6

这个问题是关于通过使用生成器和承诺来模拟async/的行为await,如下所述:https :
//gist.github.com/ChrisChares/1ed079b9a6c9877ba4b43424139b166d

这是暴露问题的最小示例:
该函数async是从上面的链接中逐字提取的。

function async(gen, context = undefined) {
	const generator = typeof gen === 'function' ? gen() : gen; // Create generator if necessary
	const { value: promise } = generator.next(context); // Pass last result, get next Promise
	if ( typeof promise !== 'undefined' ) {
		promise.then(resolved => async(generator, resolved))
		.catch(error => generator.throw(error)); // Defer to generator error handling
	}
}

function timesTwoPlusOne(num){ //multiplies argument by 2 and adds one, asynchronously
	return new Promise(function(resolve, reject) {
		async(function*(){
			//throw 'Fake exception 1' ;
			num = yield Promise.resolve( 2*num ) ; //multiply by 2, asynchronously
			//throw 'Fake exception 2' ;
			resolve( num + 1 ) ; //add one, synchronously
		}) ;
	});
}

//run an asynchronous procedure
async(function*(){
	let result ;
	try{
		result = yield timesTwoPlusOne(10) ;
	}catch(e){
		//the intention is to catch any exception happening inside `timesTwoPlusOne`
		console.log('CATCHED:', e) ;
	}
	console.log( result ) ;
}) ;
Run Code Online (Sandbox Code Playgroud)

代码原样,工作正常。它21按预期打印。
但是,在函数内部timesTwoPlusOne有两行注释掉了。

当我们取消第一个 ( throw 'Fake exception 1') 的注释并再次尝试时,该异常会向上传播并被catch整个代码中的唯一块捕获。

这正是我想要的。到现在为止还挺好。

但是现在,如果我们取消注释第二行 ( throw 'Fake exception 2') 并将第一行注释掉,则不会捕获该异常。

为什么try/catch块能够捕获第一个异常,而不能捕获第二个异常?