如何在fetch阶段停止执行.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);
}