making two fetch requests in one function with a delay in between

喜欢而已 提交于 2020-04-17 03:35:31

问题


so I have 2 endpoints, I'm making a "post" request to the first one and in the response I should get some kind of id that I use in the second request endpoint url. so I want to make a delay or something till I get the response from the first request.

req = () => {
  fetch('url', {
      method: 'POST',
      headers: {
        'Authorization': bearer,
        'Content-Type': 'application/json',
      },
      body: 
    })
    .then(response => response.json())
    .then(json => {

    })
  fetch(`url with the id I get from the first request`, {
      method: 'GET',
      headers: {
        'Authorization': bearer,
        'Content-Type': 'application/json',
      }
    })
    .then(response => response.json())
    .then(json => {

    })
}

回答1:


There is no need for a delay per se. You can place your second request in the .then of the first request. This will ensure that your second request will run only once the first one has resolved. Another important note here, is that if you need a value of the first response in order to make the second request, you can only do that in the .then of the first request because otherwise the value you need to make the second request will be out of scope. Here is your code with the required modification.

req = () => {
  fetch('url', {
    method: 'POST',
    headers: {
      'Authorization': bearer,
      'Content-Type': 'application/json',
    },
    body: 
    })
    .then(response => response.json())
    .then(json => {
      fetch(`url with json.id or whatever`, {
        method: 'GET',
        headers: {
          'Authorization': bearer,
          'Content-Type': 'application/json',
        }
      })
        .then(response => response.json())
        .then(json => {

        })
    })
}



回答2:


You can chain your second fetch request in your first request as:

req = () => {
  fetch("url", {
    method: "POST",
    headers: {
      Authorization: bearer,
      "Content-Type": "application/json"
    }
    body:
  })
    .then(response => response.json())
    .then(json => {
      fetch("url with the id I get from the first request", {
        method: "GET",
        headers: {
          Authorization: bearer,
          "Content-Type": "application/json"
        }
      })
        .then(response => response.json())
        .then(json => {});
    });
};

Or you can use async/await.

req = async () => {
    const first = await ( await fetch( "url", {
      method: "POST",
      headers: {
        Authorization: bearer,
        "Content-Type": "application/json",
      },
      body:
    } ) ).json();

    const second = await ( await fetch( `http://some.url${ first.id }` ),
    {
      method: "GET",
      headers: {
        Authorization: bearer,
        "Content-Type": "application/json",
      },
    } ).json();

    // use the variable second as your last result here.
  };


来源:https://stackoverflow.com/questions/52769701/making-two-fetch-requests-in-one-function-with-a-delay-in-between

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!