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.

what happens if we dont resolve or reject the promise的更多相关文章

  1. resolve或reject之后还需要return吗

    答案: 需要 今日碰到一个问题, 是我的同事发现的,如果不说的话可能一直没有注意到 这个代码 在reject 后还会执行, 但是谁也没有注意到, 但是不会报错, 因为当一个promise是resolv ...

  2. JavaScript异步与Promise基本用法(resolve与reject)

    Promise解决的问题相信每个前端都遇到过这样一个问题,当一个异步任务的执行需要依赖另一个异步任务的结果时,我们一般会将两个异步任务嵌套起来,这种情况发生一两次还可以忍,但是发生很多次之后,就形成了 ...

  3. promise对象里resolve和reject状态讲解及Promise.all()的使用

    首先来说下同步异步与阻塞非阻塞的概念,同步异步与阻塞非阻塞并没有关系.同步异步主要是事情做完以后,如何进行处理.或者说关注的是一种消息通信机制. 同步的情况下,是由处理消息者自己去等待消息是否被触发: ...

  4. 理解Promise函数中的resolve和reject

    看了promise的用法,一直不明白里面的resolve和reject的用法: 运行了这两段代码之后彻底理解了promise的用法: var p = new Promise(function (res ...

  5. promise、resolve、reject、拦截响应

    Promise是一个接口,它用来处理的对象具有这样的特点:在未来某一时刻(主要是异步调用)会从服务端返回或者被填充属性.其核心是,promise是一个带有then()函数的对象. 使用promise机 ...

  6. ES6---new Promise()讲解(尤其注意里面的参数resolve、reject)

    直接打印出来看看吧,console.dir(Promise). 这么一看就明白了,Promise是一个构造函数,自己身上有all.reject.resolve这几个眼熟的方法,原型上有then.cat ...

  7. resolve和reject不会终结Promise的参数函数的执行

  8. Promise.resolve()与Promise

    //Promise.resolve()和Promise.reject()常用来生成已经被决议为失败或者成功的promise案例 //Promise.reject()简单一些,不管传给它什么值,它决议为 ...

  9. ES6新特性之 promise

    新的ES6中引入了promise的概念,目的是让回调更为优雅.层层嵌套的回调会让javascript失去美感和可读性,同时javascript也推荐采用链式的方式去书写函数调用.于是Promise就应 ...

随机推荐

  1. 专题二:HTTP协议详解

    我们在用Asp.net技术开发Web应用程序后,当用户在浏览器输入一个网址时就是再向服务器发送一个HTTP请求,此时就使用了应用层的HTTP协议,在上一个专题我们简单介绍了网络协议的知识,主要是为了后 ...

  2. CSS垂直居中和水平居中的几种方法

    垂直居中 方法一 这个方法把div 的显示方式设置为表格,因此我们可以使用表格的 vertical-align属性. <!DOCTYPE html> <html lang=" ...

  3. python语言真正的奥义所在--对接32单片机

    2018-02-2720:51:24 今天晚上注定我要玩一夜这个东西,太爽了,给力! 烧写固件成功, http://blog.csdn.net/Lingdongtianxia/article/deta ...

  4. ReLU激活函数:简单之美

    出自 http://blog.csdn.net/cherrylvlei/article/details/53149381 导语 在深度神经网络中,通常使用一种叫修正线性单元(Rectified lin ...

  5. dede手机端首页点击文章内容、列表,却跳到pc端

    手机访问到手机端首页,点击列表.内容.图片等都跳到pc端,是什么原因? 查看m模板里面的index.html文件生成的代码是绝对路径(数字随机)13.html 而不是view.php?aid=13 解 ...

  6. MyBatis 之一 简介

    什么是 MyBatis ? MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...

  7. U盘安装完美的WIN7操作系统教程

    准备工作 首先备份或者在官网下载好您机器的驱动,否则完成后可能无法正常使用 ①一个有win7或者XP系统的电脑(制作启动盘用) ②一个4G以上的U盘 ③win7&win8系统包(请到官网下载或 ...

  8. Spring框架之控制反转和依赖注入

    学Spring框架必须理解控制反转和依赖注入.下面各自举一个例子,来说明控制反转和依赖注入. IOC(控制反转):应用本身创建和维护的依赖对象:现在交由外部容器(Spring)来创建和维护:这个控制权 ...

  9. C#压缩文件夹至zip,不包含所选文件夹【转+修改】

    转自园友:jimcsharp的博文C#实现Zip压缩解压实例[转] 在此基础上,对其中的压缩文件夹方法略作修正,并增加是否对父文件夹进行压缩的方法.(因为笔者有只压缩文件夹下的所有文件,却不想将选中的 ...

  10. 【Linux】CentOS安装Jenkins

    sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo sudo rpm -- ...