如何知道 fetch Api javascript 的响应状态?

how to know the state of a response with fetch Api javascript?

function boleta(){
    var check = false;
    var formboleta = new FormData();
    formboleta.append("num_boleta", data2.boletas[id].num_boleta);
    formboleta.append("created_at", data2.boletas[id].created_at);
    formboleta.append("total", data2.boletas[id].total);
    //arreglo id productos
    for(let k = 0; k < arr.length; k++){
        formboleta.append("productos", data2.boletas[id].productos[k].id);
    }
    var requestOptions = {
      method: 'POST',
      body: formboleta,
      redirect: 'follow'
    };
    let status;
    fetch("url_goes_here", requestOptions)
    .then((response) => {
      status = response.status;
      return response.json();
    })
    .catch(error => console.log('error', error));
    //---------------------------------------------
}

大家好,我想知道如何获取响应状态,如果状态为 200 或成功,则创建一个 if 并执行另一个函数。

在我想做出的第一个响应中,如果状态正常,调用另一个函数 在这种情况下,它将是 function cliente();.

函数cliente();将做另一个 post 并为这张票创建一个客户,但是当票(票我的意思是函数 boleta,它在里面创建一个 boleta(ticket))还没有完成时我不能调用它。

您可以获得 HTTP 请求的状态,如

.then((response) => {
    status = response.status;
    return response.json();
  })

如果你想在第二个 then 中获得状态,那么创建一个变量并在获得响应后存储状态

let status;
fetch("https://jsonplaceholder.typicode.com/todos/1")
  .then((response) => {
    // Get status using response.status
    status = response.status;
    console.log(`status in first then ${status}`);
    return response.json();
  })
  .then((json) => {
    // Get status in after the first .then
    console.log(`status in second then 4 ${status}`);
    console.log(console.log(json));
  });