如何在fetch阶段停止执行.then阶段的脚本?

问题描述 投票:0回答:1

如何在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);
    }
javascript fetch
1个回答
0
投票
 throw "Stop!";

您可以拒绝返回的承诺,然后进一步执行.then处理程序。

或者你只是将两个.thens合并为一个:

if (html && html.length) {
  $('#modal-content').html('');
  $('#modal-content').append(html);
  // no need to STOP here
} else if (status === 200) {
  document.location.reload(true);
} 
© www.soinside.com 2019 - 2024. All rights reserved.