"use strict";
let promiseCount = 0;
function testPromise() {
const thisPromiseCount = ++promiseCount;
const log = document.getElementById("log");
// begin
log.insertAdjacentHTML("beforeend", `${thisPromiseCount}) Started<br>`);
// We make a new promise: we promise a numeric count of this promise,
// starting from 1 (after waiting 3s)
const p1 = new Promise((resolve, reject) => {
// The executor function is called with the ability
// to resolve or reject the promise
log.insertAdjacentHTML(
"beforeend",
`${thisPromiseCount}) Promise constructor<br>`
);
// This is only an example to create asynchronism
setTimeout(() => {
// We fulfill the promise
resolve(thisPromiseCount);
}, Math.random() * 2000 + 1000);
});
// We define what to do when the promise is resolved with the then() call,
// and what to do when the promise is rejected with the catch() call
p1.then((val) => {
// Log the fulfillment value
log.insertAdjacentHTML("beforeend", `${val}) Promise fulfilled<br>`);
}).catch((reason) => {
// Log the rejection reason
console.log(`Handle rejected promise (${reason}) here.`);
});
// end
log.insertAdjacentHTML("beforeend", `${thisPromiseCount}) Promise made<br>`);
}
const btn = document.getElementById("make-promise");
btn.addEventListener("click", testPromise);
<button id="make-promise">Make a promise!</button>
<div id="log"></div>
参考承诺,我如何手动生成被拒绝的承诺,或者在什么情况下这段代码会运行拒绝,请提供一些实用的概念,以便我可以看到以下错误消息,请参考代码片段: console.log(Handle rejected promise (${reason}) here.
);
发布于 2022-10-09 22:16:14
尝试通过在诺言执行器函数中运行的超时代码中生成被拒绝的承诺来测试允诺拒绝--即传递给全局Promise
构造函数作为其参数的函数中的意思。
一种简单的方法可能是拒绝奇数承诺,而用固定值解决偶数承诺。如果您需要一个示例,下面的代码会这样做。它还会在HTML中记录拒绝承诺。
"use strict";
let promiseCount = 0;
function testPromise() {
const thisPromiseCount = ++promiseCount;
const log = document.getElementById("log");
// begin
log.insertAdjacentHTML("beforeend", `${thisPromiseCount}) Started<br>`);
// We make a new promise: we promise a numeric count of this promise,
// starting from 1 (after waiting 3s)
const p1 = new Promise((resolve, reject) => {
// The executor function is called with the ability
// to resolve or reject the promise
log.insertAdjacentHTML(
"beforeend",
`${thisPromiseCount}) Promise constructor<br>`
);
// This is only an example to create asynchronism
setTimeout(() => {
// We fulfill the promise on odd calls, reject on even calls
(thisPromiseCount % 2 ? reject : resolve) (thisPromiseCount);
}, Math.random() * 2000 + 1000);
});
// We define what to do when the promise is resolved with the then() call,
// and what to do when the promise is rejected with the catch() call
p1.then((val) => {
// Log the fulfillment value
log.insertAdjacentHTML("beforeend", `${val}) Promise fulfilled<br>`);
}).catch((reason) => {
// Log the rejection reason
console.log(`Handle rejected promise (${reason}) here.`);
log.insertAdjacentHTML("beforeend", `${thisPromiseCount}) Promise rejected<br>`)
});
// end
log.insertAdjacentHTML("beforeend", `${thisPromiseCount}) Promise made<br>`);
}
const btn = document.getElementById("make-promise");
btn.addEventListener("click", testPromise);
<button type="button" id="make-promise">#make-promise</button>
<div id="log"></div>
https://stackoverflow.com/questions/74008292
复制相似问题