博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
what happens if we dont resolve or reject the promise
阅读量:5957 次
发布时间:2019-06-19

本文共 2880 字,大约阅读时间需要 9 分钟。

 

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.

转载地址:http://thexx.baihongyu.com/

你可能感兴趣的文章
SQL Server代理(3/12):代理警报和操作员
查看>>
Linux备份ifcfg-eth0文件导致的网络故障问题
查看>>
2018年尾总结——稳中成长
查看>>
JFreeChart开发_用JFreeChart增强JSP报表的用户体验
查看>>
度量时间差
查看>>
通过jsp请求Servlet来操作HBASE
查看>>
Shell编程基础
查看>>
Shell之Sed常用用法
查看>>
3.1
查看>>
校验表单如何摆脱 if else ?
查看>>
<气场>读书笔记
查看>>
web安全问题分析与防御总结
查看>>
Centos下基于Hadoop安装Spark(分布式)
查看>>
3D地图的定时高亮和点击事件(基于echarts)
查看>>
mysql开启binlog
查看>>
设置Eclipse编码方式
查看>>
分布式系统唯一ID生成方案汇总【转】
查看>>
并查集hdu1232
查看>>
Mysql 监视工具
查看>>
从前后端分离到GraphQL,携程如何用Node实现?\n
查看>>