简体   繁体   中英

Async Function returns Promise pending

I don't understand why i get Promise { <pending> } since i used async/await

this is my code

const  fetch = require("node-fetch")

function getRandomPokemon() {
    var pokemonID = Math.floor(Math.random() * 851);
    console.log("The Pokemon Id is " + pokemonID);
    return pokemonID
}

async function getJSON() {
    let response = await fetch('https://pokeapi.co/api/v2/pokemon/'+ pokemonID);
    var json = await response.json
}

var pokemonID = getRandomPokemon()
var json = getJSON()
console.log(json)

Three problems:

  1. getJSON() doesn't return anything. It needs to end with return json; .
  2. Declaring a function async makes it return a promise. If you want to get the return value directly rather than a promise, you have to call it with await getJSON() . You can only do that if you're calling it from another async function.
  3. You need to call response.json : var json = response.json();

All async functions return a promise - always. Using await inside an async function suspends the execution of that function until the promise you are awaiting resolves or rejects, but an async function is not blocking to the outside world. An await does not suspect execution of the whole js interpreter.

At the point of the first await in the async function your function returns a promise back to the caller and the caller continues to execute. That promise is then resolved or rejects when the function eventually completes its work sometime in the future.

I don't understand why i get Promise { } since i used async/await

Because all async functions return a promise.


There are numerous issues in your code:

  1. Your getJSON() function has no return value. That means that the promise it returns resolves to undefined so there's not way to communicate back its value.
  2. await response.json needs to be await response.json() .
  3. pokemonID should be passed as an argument, not just stuffed into a higher scoped variable.
  4. When calling getJSON() , you have to use .then() or await on it to get the resolved value from the returned promise.

You may also notice that your getJSON() function has no return value. That means that the promise it returns also resolves to undefined . And, await response.json needs to be await response.json() .

Your code needs to be more like this:

async function getJSON(id) {
    const response = await fetch('https://pokeapi.co/api/v2/pokemon/'+ id);
    const json = await response.json();
    return json;
}

const pokemonID = getRandomPokemon();
getJSON(pokemonID).then(data => {
    console.log(data);
}).catch(err => {
    console.log(err);
});

response.json should be a function.

On your getJSON function definition, you are doing await response.json . Change it to await response.json() .

Notice the parenthesis after json() .

Since the getJSON function returns promise, you have to await the response

On your second last line, add var json = await getJSON();

furthermore, in order to use await , you must wrap your code in an async function

Wrap your last and second-last line in an async function

async function resolveJSON(){
    var json = await getJSON(); // waits for the promise to fulfill
    console.log(json);
}

Just a few changes

  1. Add the return statement to the getJSON() function
  2. Use the.json() function to get the json from the response in the getJSON()
  3. One way to handle a promise is to use.then(). You could also use await but make sure that it's scoped inside an await block.
const fetch = require('node-fetch');

function getRandomPokemon() {
  var pokemonID = Math.floor(Math.random() * 851);
  console.log('The Pokemon Id is ' + pokemonID);
  return pokemonID;
}

async function getJSON() {
  let response = await fetch('https://pokeapi.co/api/v2/pokemon/' + pokemonID);
  var json = await response.json();
  return json
}

var pokemonID = getRandomPokemon();
getJSON().then((json) => {
  console.log(json);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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