对于下面这段 js
function async3() {
createPromise()
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
})
.finally(() => {
console.log('finally');
});
}
其对应 async/await
版本是
async function async2() {
try {
const ret = await createPromise();
console.log(ret);
} catch (e) {
console.error(e);
} finally {
console.log('finally');
}
}
经 babel 转为 ES5 后,除了必要的 babel-polypill,会生成以下代码,其中 _asyncToGenerator
不会多次生成。
写法上直观了的,但在浏览器端,还是有些代价,生成的代码会多出不少。