gpt4 book ai didi

javascript - node-js 中的异步/等待获取

转载 作者:数据小太阳 更新时间:2023-10-29 05:45:00 26 4
gpt4 key购买 nike

const fetch = require("node-fetch"); 

async function getPokemon() {
const response = await fetch('https://pokeapi.co/api/v2/pokemon/1');
console.log(response);
return response;
}

getPokemon();

我不确定这是否有效。我回来了:

Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]:
{ body:
Gunzip {
_readableState: [ReadableState],
readable: true,
_events: [Object],
_eventsCount: 7,
_maxListeners: undefined,
_writableState: [WritableState],
writable: true,
allowHalfOpen: true,
_transformState: [Object],
bytesWritten: 0,
_handle: [Zlib],
_hadError: false,
_writeState: [Uint32Array],
_outBuffer:
<Buffer 7b 22 61 62 69 6c 69 74 69 65 73 22 3a 5b 7b 22 61 62 69 6c 69 74 79 22 3a 7b 22 6e 61 6d 65 22 3a 22 63 68 6c 6f 72 6f 70 68 79 6c 6c 22 2c 22 75 72 ... >,
_outOffset: 0,
_level: -1,
_strategy: 0,
_chunkSize: 16384,
_defaultFlushFlag: 2,
_finishFlushFlag: 2,
_nextFlush: -1,
_info: undefined },
disturbed: false,
error: null },
[Symbol(Response internals)]:
{ url: 'https://pokeapi.co/api/v2/pokemon/1',
status: 200,
statusText: 'OK',
headers: Headers { [Symbol(map)]: [Object] } } }

这是我要找的对象吗?或者这只是我回来的 promise ?

我期待的是这样的:

https://pokeapi.co/

height:7
held_items:
id:1
is_default:true
location_area_encounters:"https://pokeapi.co/api/v2/pokemon/1/encounters"
name:"bulbasaur"
order:1
name:"bulbasaur"
url:"https://pokeapi.co/api/v2/pokemon-species/1/"
// and more

我不确定可能是什么问题,也许我对 Node 中的异步等待有误解?很抱歉成为初学者,在此先感谢您!

最佳答案

is this the object I'm looking for?

响应对象。

您想要从响应对象中提取响应主体并将其解析为 JSON 的结果。

  const response = await fetch('https://pokeapi.co/api/v2/pokemon/1');
const data = await response.json();
console.log(data);

Or is this just the promise that I get back?

没有。您等待 promise 。

关于javascript - node-js 中的异步/等待获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54555778/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com