Как остановить выполнение скрипта на этапе .hen при получении? - PullRequest
0 голосов
/ 11 марта 2019

Как мне остановить выполнение скрипта на этапе .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);
    }

1 Ответ

0 голосов
/ 11 марта 2019
 throw "Stop!";

Вы можете отклонить возвращенное обещание, тогда дальнейшие обработчики .then не будут выполняться.

Или вы просто объединяете два .then в один:

if (html && html.length) {
  $('#modal-content').html('');
  $('#modal-content').append(html);
  // no need to STOP here
} else if (status === 200) {
  document.location.reload(true);
} 
...