Javascript 获取 API 无法控制即将到来的响应

Javascript Fetch API can't control the coming response

我正在尝试从给定的 URL 中获取一些信息。我将 URL 分配给了一个名为 URL 的常量。我使用 fetch api 以 JSON 格式从源中获取信息。我无法控制即将到来的 information.Here 是我的代码;

const fetch = require("cross-fetch");
const URL = "https://anapioficeandfire.com/api/books"


// Important: Don't change the function name
const getBooks = async () => {
  // Your code goes here
  
  
  const response = await fetch(`${URL}`)
    .then(res => res.json())
    .then(data => console.log(data));
  const books = await response.json();
  return books;
}

getBooks().then(books => console.log(books))

This is the response from the code that I wrote
我只需要
{
名称:“...”,
numberOfPages: "....",
发布:“......”,
},
{
名称:“...”,
numberOfPages: "....",
发布:“......”,
},
....

Return 您需要从 api 响应中获得的值。 以下是您的操作方法:

const url = "https://anapioficeandfire.com/api/books";

const callApi = async () => {
const resp = await fetch(url);
const finalRes = await resp.json();
return finalRes.map((res) => {
    return {
    name: res.name,
    numberOfPages: res.numberOfPages,
    released: res.released,
    };
});
};

(async () => console.log(await callApi()))();

来自浏览器控制台的结果集:

此外,在等待 promise 实现时,您可以执行 .then() 或使用 await。 他们都解决了同一个目的。