调用response.json()时获取错误

时间:2018-05-22 05:05:10

标签: javascript promise fetch

在我的API类中,我有一个如下代码,

let response;
try {
  response = await fetch(requestUrl, {
    method: 'POST',
    headers,
    timeout: REQUEST_TIMEOUT_MS,
    body: JSON.stringify({
      id,
      accNumber: acctId,
      invNumber: invId,
      fromDate,
      toDate
    })
  });
} catch (error) {
  throw new Error(
    'Unexpected error occurred while invoking ATOM Invoice API',
    error
  );
}
foo(cloneDeep(response));
const result = await response.json();
--- some more code ---

然后在另一个课程中,我实施了foo方法,

async foo(response){
    let result;
    let errorDescription;
    try {
      result = await response.json();
    } catch (error) {
      logger.error(JSON.stringify(error));
    }
    --- some more code ---
}

运行上面的代码时,我从foo函数抛出了以下错误:

{
    "name": "FetchError",
    "message": "response timeout at <my request URL in here> over limit: 10000",
    "type": "body-timeout"
}

我想知道为什么会这样,我该怎么做才能避免这种情况。提前谢谢!

0 个答案:

没有答案