Promise.finally()
方法返回一个Promise。在promise结束时,无论结果是fulfilled或者是rejected,都会执行finally(),避免了同样的语句需要在then()和catch()中各写一次的情况。
new Promise((resolve, reject) => {
setTimeout(function () {
reject("400");
}, 1000);
}).then((result) => {
console.log("成功:" + result);
}).catch((reason) => {
console.log("失败:" + reason);
}).finally(() => {
console.log("结束");
});
正则表达式命名捕获组
const reDate = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/;
const match = reDate.exec("2020-08-27");
console.log(match.groups.year);
console.log(match.groups.month);
console.log(match.groups.day);
const d = "2020-08-27";
console.log(d.replace(reDate, "$<month>-$<day>-$<year>"));