好好爱自己!

what happens if we dont resolve or reject the promise

https://stackoverflow.com/questions/36734900/what-happens-if-we-dont-resolve-or-reject-the-promise

 

I have a scenario where I am returning a promise. The promise basically give the response of a ajax request.

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

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

My code looks something like this

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});
                    })    
                }
            }

        })
    });
}

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.

Thanks.

----------------------------------------------------------------

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.

The real consequence here is what does that mean to the consumer of the promise if its state is never changed? Any .then() 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.

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.

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.

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.

posted @   立志做一个好的程序员  阅读(367)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2016-11-15 windows 杀进程软件
2016-11-15 git 使用系列(一)—— git stash 的使用
2016-11-15 windows cmd 命令大全
2016-11-15 javascript逻辑运算符“||”和“&&”
2016-11-15 js大牛博客

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示