练习一些我承诺的 JS 教程,以便我可以拒绝或决心测试不同的场景。 使用 myPromise4.then() 时,控制台中出现 Uncaught... 错误。 当仅使用 .then() 而不是 myPromise4.then() 时,控制台中没有错误。我本以为这两种形式是等效的。我知道答案一定在互联网上的某个地方,但我无法正确地表达我的查询来找到它。
let myPromise4 = new Promise((resolve, reject) => {
if (confirm("myPromise4 OK for resolve, CANCEL for reject myPromise4")) {
resolve("you chose to resolve myPromise4");
} else {
reject(new Error("you chose to reject myPromise4"));
}
})
.finally(() => console.log("finally myPromise4"))
.then((result) => {
console.log("myPromise4 resolved");
}, null);
myPromise4.catch(
//RELEVANT PIECE
function () {
console.log("error was caught for myPromise4");
},
);
myPromise4.finally(() => console.log("finally myPromise4"));
产生这个:
let myPromise4 = new Promise((resolve, reject) => {
if (confirm("OK for resolve, CANCEL for reject myPromise4")) {
resolve("you chose to resolve myPromise4");
} else {
reject(new Error("you chose to reject myPromise4"));
}
})
.finally(() => console.log("finally myPromise4"))
.then((result) => {
console.log("myPromise4 resolved");
}, null)
.catch(
//RELEVANT PIECE
function () {
console.log("error was caught for myPromise4");
},
);
myPromise4.finally(() => console.log("finally myPromise4"));
产生这个: