首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我怎么能看到一个承诺被拒绝了

我怎么能看到一个承诺被拒绝了
EN

Stack Overflow用户
提问于 2022-10-09 20:56:15
回答 1查看 37关注 0票数 -1

代码语言:javascript
运行
复制
"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);
代码语言:javascript
运行
复制
<button id="make-promise">Make a promise!</button>
<div id="log"></div>

参考承诺,我如何手动生成被拒绝的承诺,或者在什么情况下这段代码会运行拒绝,请提供一些实用的概念,以便我可以看到以下错误消息,请参考代码片段: console.log(Handle rejected promise (${reason}) here.);

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-09 22:16:14

尝试通过在诺言执行器函数中运行的超时代码中生成被拒绝的承诺来测试允诺拒绝--即传递给全局Promise构造函数作为其参数的函数中的意思。

一种简单的方法可能是拒绝奇数承诺,而用固定值解决偶数承诺。如果您需要一个示例,下面的代码会这样做。它还会在HTML中记录拒绝承诺。

代码语言:javascript
运行
复制
"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);
代码语言:javascript
运行
复制
<button type="button" id="make-promise">#make-promise</button>
<div id="log"></div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74008292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档