简体   繁体   English

如何在 HTTP 请求中放置缓冲区?

[英]How to put a buffer in an HTTP request?

I'm trying to put a buffer in a request because I have a list of data to import.我试图在请求中放置一个缓冲区,因为我有一个要导入的数据列表。 I want to have success request after one another.我想要一个接一个的成功请求。 The problem I'm encountering is that it waits to upload all data of the request.我遇到的问题是它等待上传请求的所有数据。

Here is the sample data:这是示例数据:

[
  {
    "contacts": "dsds@dsd.com",
    "recipient": "dsd@dsd.com",
    "date_sent": "07/08/2020 17:05:04",
    "subject": "repurchase"
  },
  {
    "contacts": "asd@ret.com",
    "recipient": "test@yahoo.com",
    "date_sent": "07/10/2020 17:31:51",
    "subject": "biz"
  },
  {
    "contacts": "we@sdf.com",
    "recipient": "abc@yahoo.com",
    "date_sent": "07/09/2020 13:02:54",
    "subject": "rock"
  }
];
const createEngage = async(body) => {
  const BASE_URL = '/api/import'
  var requestOptions = {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        "Content-Type": "application/json"
      },
      body: body
    };

  fetch(BASE_URL, requestOptions)
    .then(response => response.text())
    .then(async result => {
      console.log(result);
    })
    .catch(error => console.log('error', error));
}

What you probably want to do is to loop over your data and use async / await to wait at each iteration.您可能想要做的是遍历您的数据并使用async / await在每次迭代时等待。 Your implementation of your asynchronous function currently does not await anything.您的异步函数的实现目前不await任何东西。 Instead it should await the fetch request and the decoding of the body with response.text() .相反,它应该使用response.text() await fetch请求和正文的解码。

Check the response for errors and wrap the fetch request in a try...catch block.检查响应是否有错误并将fetch请求包装在try...catch块中。 If an error occurs then the catch block will be executed.如果发生错误,则将执行catch块。 Otherwise check theresponse object for any states or errors you want to include.否则,请检查response对象是否有您想要包含的任何状态或错误。

const data = [
  {
    "contacts": "dsds@dsd.com",
    "recipient": "dsd@dsd.com",
    "date_sent": "07/08/2020 17:05:04",
    "subject": "repurchase"
  },
  {
    "contacts": "asd@ret.com",
    "recipient": "test@yahoo.com",
    "date_sent": "07/10/2020 17:31:51",
    "subject": "biz"
  },
  {
    "contacts": "we@sdf.com",
    "recipient": "abc@yahoo.com",
    "date_sent": "07/09/2020 13:02:54",
    "subject": "rock"
  }
];
const BASE_URL = '/api/import'

/**
 * Sends a request for each individual item.
 */
const createEngage = async body => {
  const requestOptions = {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
    body
  };
  try {
    const response = await fetch(BASE_URL, requestOptions);
    if (!response.ok) {
      alert('Your request has failed');
      return null;
    }
    const text = await response.text();
    return text;
  } catch(error) {
    alert('Your request caused an error');
  }
};

/**
 * Loop over each item and call createEngage.
 * Wait for the request to finish and continue.
 */
const createMultipleEngages = async data => {
  for (const item of data) {
    const result = await createEngage(item); // This will make the loop wait every time.
    console.log(result);
  }
};

// Call the function and start looping.
createMultipleEngages(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM