如果您不解决或拒绝诺言,会发生什么? [英] What happens if you don't resolve or reject a promise?

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

问题描述

我有一个要兑现诺言的情况. 答应基本上是由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不会出现内部问题,但不是承诺的设计方式,通常也不是承诺的使用者希望它们运行的​​方式.

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,因此我正在重定向到登录页面. 承诺既不会解决也不会被拒绝.

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天全站免登陆