javascript - 如何停止脚本在 .then 阶段执行?
问题描述
如何停止脚本在 .then 阶段执行?如果 response 和 response.text 不为空,我希望脚本停止执行
fetch(url, {
method: 'POST',
body: data,
credentials: 'include',
})
.then(response => response.text())
.then(html => {
if (html && html.length) {
$('#modal-content').html('');
$('#modal-content').append(html);
//STOP here.
}
})
.then(({ status }) => {
if (status === 200) {
document.location.reload(true);
}
})
UPD:我最终写了另一个 if 条件
if (status === 200 && $('#notavailable-modal').length == 0) {
document.location.reload(true);
}
解决方案
throw "Stop!";
您可以拒绝返回的承诺,然后.then
不会执行进一步的处理程序。
或者您只是将两个.then
s 合并为一个:
if (html && html.length) {
$('#modal-content').html('');
$('#modal-content').append(html);
// no need to STOP here
} else if (status === 200) {
document.location.reload(true);
}
推荐阅读
- angular - 角度指令中的css选择器
- sql-server - SQL Server - 链接服务器消失
- java - 从给定角度计算目标像素
- intellij-idea - 在 Intellij 中构建 jEdit
- opengl - 从 mipmap 纹理中读取像素
- c++ - 部分模板专业化失败
- javascript - 我的表单没有提交到 firebase firestore
- ubuntu - Hyperledger Fabric 网络中的 CouchDB 错误 - 数据库不存在
- java - 通过参数继承 Spring REST 控制器
- botframework - 即使在用户发送任何消息之前,如何在团队中获得欢迎消息