const myPromise = Promise.resolve("Woah some cool data");
(async () => {
try {
console.log(await myPromise);
} catch {
throw new Error(`Oops didn't work`);
} finally {
console.log("Oh finally!");
}
})();
在 try
块区,我们打印 myPromise
变量的 awaited 值: "Woah some cool data"
。因为try
块区没有错误抛出,catch
块区的代码并不执行。finally
块区的代码 总是 执行,"Oh finally!"
被输出。