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. 303 Range Sum Query - Immutable 区域和检索 - 不可变

    给定一个数组,求出数组从索引 i 到 j  (i ≤ j) 范围内元素的总和,包含 i,  j 两点.例如:给定nums = [-2, 0, 3, -5, 2, -1],求和函数为sumRange() ...

  2. SQLServer2005 维护计划 无法删除

    1.查看"维护计划"对象的ID use msdbselect * from sysmaintplan_plansselect * from sysmaintplan_logsele ...

  3. leetcode464 Can I Win

    思路: 博弈. 实现: class Solution { public: bool dfs(int cur, int len, int sum, int des, vector<int>& ...

  4. Android中 string.xml资源 如何添加参数?

    在android 开发,我们通常会用string.xml资源去设置textview等控件的字符串.而值一般是与程序的运行结果无关的. 但有时需要根据运行的结果来显示到控件中,这时字符串资源就不能写死了 ...

  5. Caffe RPN:把RPN网络layer添加到caffe基础结构中

    在测试MIT Scene Parsing Benchmark (SceneParse150)使用FCN网络时候,遇到Caffe错误. 遇到错误:不可识别的网络层crop 网络层 CreatorRegi ...

  6. 部署bugzilla(bugzilla+apache+mysql+linux)

    工作原因,需要部署bugzilla.在此,容我新造个轮子.官方轮子:https://bugzilla.readthedocs.org/en/latest/installing/quick-start. ...

  7. HDU_1874_畅通工程续_最短路问题

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  8. Java基础(三)--final关键字

    final通常是指"不可改变的",例如我们使用的常量 通常可以有三种使用情况: 一.final修饰数据 如果final修饰数据,也就是通常所说的常量,从字面上看,常量就是不能修改的 ...

  9. oracle_backup

    #!/bin/bash DAYS=`date +"%Y%m%d"` . /home/oracle/.bash_profile # /home/opt/oracle/11g/bin/ ...

  10. Django - 自定义filter

    自定义filter 自定义filter时,使用装饰器fileter 在html中,使用传参方式为: 参数1|函数名:参数2 并且函数和参数之间,不能有空格,如果有空格,会报错. filter和simp ...