10

如果你自认熟悉 async...await,来猜一下这个代码的运行结果

 4 years ago
source link: https://www.v2ex.com/t/789253
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  JavaScript

如果你自认熟悉 async...await,来猜一下这个代码的运行结果

  autoxbc · 1 天前 · 3743 次点击

姐妹篇
如果你自认熟悉 Promise,来猜一下这个代码的运行结果

要用猜的,别偷偷用浏览器运行

const Err = async () => {
	throw new Error(42);
};

const Obj = {
	async A (){
		try {
			return Err();
		} catch {
			console.log('A');
		}
	},
	async B (){
		try {
			await Err();
		} catch {
			console.log('B');
		}
	},
	async C (){
		try {
			Err();
		} catch {
			console.log('C');
		}
	},
};

( async () => {
	for( const key in Obj )
	{
		try {
			await Obj[key]();
		} catch {
			console.log('D');
		}
	}
} )();

第 1 条附言  ·  1 天前

1. 这是标准的异步函数错误捕获,可不是茴字写法什么的
2. 回复中已经有朋友认真解答知识点,这题的目的达到了

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK