Skip to content

Instantly share code, notes, and snippets.

@suica
Created July 17, 2022 04:14
Show Gist options
  • Save suica/98f72cdd7f44350297e8eb07e55982b9 to your computer and use it in GitHub Desktop.
Save suica/98f72cdd7f44350297e8eb07e55982b9 to your computer and use it in GitHub Desktop.
一道考察非常深入的promise面试题
/**
* 题目描述:
* 你在字节的面试中遇到了这样的代码。
* 1. 这份代码的输出是什么?
* 2. 这份代码有什么问题?如何修复这个问题?
*/
async function a() {
console.log('1');
const z = new Promise((resolve) => {
console.log('3');
resolve('4');
}).then((data) => { setTimeout(() => { throw Error(data) }); })
.then(() => { console.log('then'); })
.finally(() => { console.log('finally') })
.catch((e) => { console.log('catch') })
console.log('2');
return z;
}
async function b() {
try { console.log(await (a())); } catch (e) { console.log('error'); }
}
console.log(typeof b());
console.log('6')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment