16
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();

I am not sure this is working. I get back:

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] } } }

is this the object I'm looking for? Or is this just the promise that I get back?

I was expecting something like:

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

I'm not sure what the problem might be, maybe I am misunderstanding something about async await in node? Sorry for being a beginner, thanks a lot in advance!

R. Kohlisch
  • 2,823
  • 6
  • 29
  • 59

1 Answers1

50

is this the object I'm looking for?

It is the response object.

You want the results of extracting the response body from the response object and parsing it as 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?

No. You awaited the promise.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • amazing, thank you. I will select this answer. But what if now I'd like to return the data from my function, and return it outside of the function via `console.log(getPokemon());` would that work? It doesn't seem to. – R. Kohlisch Feb 06 '19 at 14:30
  • 1
    See https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Quentin Feb 06 '19 at 14:32
  • can you explain why there are two awaits? – john k Jul 26 '23 at 21:35
  • @johnk because there are two promises. – Quentin Jul 27 '23 at 06:01
  • @Quentin await, by definition, resolves the promise. You won't get a promise back if you use await, you'll get the response or the rejection. At least, that's what Ive been taught – john k Jul 27 '23 at 18:40
  • @johnk — “The (singular) promise” does not make sense. As I said, there are **two** promises. Check the documentation for what `fetch()` returns and for what `json()` returns. – Quentin Jul 27 '23 at 22:26