javascript - 如何一次取消所有正在进行的 Promise
问题描述
Promise
想象一个通过该函数发出数百个请求 () 的工具:
function send(requestPromise, successFn, failureFn) {
incrementCounters();
requestPromise()
.then(response => successFn(response.headers, response.data))
.catch(() => failureFn())
.finally(() => decrementCounters());
}
现在,假设被联系的服务器决定拒绝所有后续请求,因为客户端点击了API-Rate Limit
.
假设客户端想要停止等待所有请求:客户端如何让所有正在进行的 Promise 失败?
因此,约束是:
- 不应等待来自服务器的响应(
then()
不应执行) - 取消应该同时触发
catch()
和finally()
调用 - 即使
Promise
已经排队但尚未触发对服务器的调用也应该被取消
解决方案
推荐阅读
- grid - 网格中的元素位置
- javascript - 如何使用 html 和 javascript 重定向到另一个页面
- reactjs - 如何从模式中提交表单,然后确保数据在前端刷新?
- events - do databases not exist under event stores (like kafka)? Do we not query data from databases anymore?
- angular - 将所有页面中的默认文本替换为自定义文本
- python - PythonAnywhere:SystemError:参数说明符多于关键字列表条目(剩余格式:'s#|O!O!O!')
- java - 在 JavaFX TreeView 中手动提交编辑
- java - 为什么 Roy 和头像hackerearth 的测试用例失败了?
- google-calendar-api - Google 日历 - 外部活动来宾的自动电子邮件通知
- html - 我应该使用 role="note" 作为输入的帮助文本吗?