如果您不解决或拒绝承诺会怎样? [英] What happens if you don't resolve or reject a promise?

查看:16
本文介绍了如果您不解决或拒绝承诺会怎样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回承诺的场景.promise 基本上是由 ajax 请求触发的.

I have a scenario where I am returning a promise. The promise is basically triggered by an ajax request.

在拒绝承诺时,它会显示一个错误对话框,表明存在服务器错误.

On rejecting the promise it shows an error dialog that there is a server error.

我想做的是当响应代码是 401 时,我既不想解决承诺也不想拒绝它(因为它已经显示了错误对话框).我只想重定向到登录页面.

What I want to do is when the response code is 401, I neither want to resolve the promise nor reject it (because it already shows the error dialog). I want to simply redirect to the login page.

我的代码如下所示:

function makeRequest(ur, params) {
  return new Promise(function (resolve, reject) {
    fetch(url, params).then((response) => {
      let status = response.status;    
      if (status >= 200 && status < 300) {
        response.json().then((data) => {
          resolve(data);
        });
      } else {
        if (status === 401) {
          redirectToLoginPage();
        } else {
          response.json().then((error) => {
            if (!error.message) {
              error.message = constants.SERVER_ERROR;
            }
            reject({ status, error });
          });
        }
      }
    });
  });
}

如您所见,如果状态为 401,我将重定向到登录页面.承诺既没有解决也没有拒绝.

As you can see, if the status is 401, I am redirecting to the login page. The promise is neither resolved nor rejected.

这段代码可以吗,或者有没有更好的方法来完成这个?

Is this code OK, or is there any better way to accomplish this?

推荐答案

promise 只是 Javascript 中具有属性的对象.它没有魔法.因此,未能解决或拒绝承诺只是无法将状态从待定"更改为其他任何状态.这不会在 Javascript 中引起任何基本问题,因为 promise 只是一个常规的 Javascript 对象.如果没有代码保留对承诺的引用,承诺仍然会被垃圾收集(即使仍然挂起).

A promise is just an object with properties in Javascript. There's no magic to it. So failing to resolve or reject a promise just fails to ever change the state from "pending" to anything else. This doesn't cause any fundamental problem in Javascript because a promise is just a regular Javascript object. The promise will still get garbage collected (even if still pending) if no code keeps a reference to the promise.

这里的真正后果是,如果承诺的状态永远不会改变,那么这对承诺的消费者意味着什么?任何用于解析或拒绝转换的 .then().catch() 侦听器都不会被调用.大多数使用 Promise 的代码都希望它们在未来的某个时刻解决或拒绝(这就是为什么首先使用 Promise).如果他们不这样做,那么该代码通常永远无法完成其工作.

The real consequence here is what does that mean to the consumer of the promise if its state is never changed? Any .then() or .catch() listeners for resolve or reject transitions will never get called. Most code that uses promises expects them to resolve or reject at some point in the future (that's why promises are used in the first place). If they don't, then that code generally never gets to finish its work.

您可能有一些其他代码来完成该任务的工作,并且承诺只是被放弃而没有做它的事情.如果你这样做的话,Javascript 中没有内部问题,但这不是 Promise 的设计方式,通常也不是 Promise 的使用者期望它们工作的方式.

It's possible that you could have some other code that finishes the work for that task and the promise is just abandoned without ever doing its thing. There's no internal problem in Javascript if you do it that way, but it is not how promises were designed to work and is generally not how the consumer of promises expect them to work.

如您所见,状态是否为 401,我正在重定向到登录页面.Promise 既没有解决也没有拒绝.

As you can see if the status is 401, I am redirecting to login page. Promise is neither resolved nor rejected.

这个代码好吗?或者有没有更好的方法来实现这一点.

Is this code OK? Or is there any better way to accomplish this.

在这种特殊情况下,一切正常,重定向是一种特殊且独特的情况.重定向到新的浏览器页面将完全清除当前页面状态(包括所有 Javascript 状态),因此使用重定向的快捷方式并保留其他未解决的问题完全没问题.当新页面开始加载时,系统将完全重新初始化您的 Javascript 状态,以便清除任何尚未完成的承诺.

In this particular case, it's all OK and a redirect is a somewhat special and unique case. A redirect to a new browser page will completely clear the current page state (including all Javascript state) so it's perfectly fine to take a shortcut with the redirect and just leave other things unresolved. The system will completely reinitialize your Javascript state when the new page starts to load so any promises that were still pending will get cleaned up.

这篇关于如果您不解决或拒绝承诺会怎样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆