async await 不等待对象填充 axios.get

async await not waiting for object to fill axios.get

我有一个名为 move_dict 的对象,我正在使用方法 get_learned_by_pokemon() 来填充它。在 getPokes() 中,我调用 get_learned_by_pokemon() 并期望 move_dict 被填充。然而它是空的。

function move(){
    let move_dict = {}
    let db_data = JSON.parse(fs.readFileSync('pokes.json', 'utf8'));
    async function get_learned_by_pokemon(curr_move, move_dict, current_poke){
        response = await axios.get('https://pokeapi.co/api/v2/move/'.concat(curr_move))

        let learned_by_pokemons = []
        for (let k = 0; k < response.data.learned_by_pokemon.length; k++){
            learned_by_pokemons.push(response.data.learned_by_pokemon[k].name)
        }
        // pass
        if (learned_by_pokemons.includes(current_poke)){move_dict[current_poke].pass.push(curr_move)}
        // fail
        else{move_dict[current_poke].fail.push(curr_move)}

    }
    function getPokes(){
        //iterate through all pokemon
        for (let i = 0; i < db_data.length; i++){
            let current_poke = db_data[i].name
            let moves = db_data[i].moves
            move_dict[current_poke] = {pass: [], fail: []}
            //iterate through all moves of pokemon
            for (let j = 0; j < moves.length; j++){
                let curr_move = moves[j]
                //get data on current move
                get_learned_by_pokemon(curr_move, move_dict, current_poke)
            }
        }
    }

    getPokes()
}

我也在 Axios.get() 之前使用了 await .我想知道为什么 move_dict 没有填充,我想在不使用 setTimeout()

的情况下解决这个问题

您的代码有 2 个错误。 1:你在哪里打电话 getPokes() ? 2: get_learned_by_pokemon 是异步函数所以如果你想等待它完成你必须使用 await

OP 似乎以 json 描述口袋妖怪的文件开头。从代码来看,该文件似乎至少包含以下内容...

[
  { name: 'nameA', moves: [ 1, 2, 3, ... ] },
  { name: 'nameB', moves: [ 3, 4, 5, ... ] },
  ...
]

看起来有一个 api 端点采取一个动作(“id 或 name”)和 returns,除此之外,还有一个“学习”该动作的口袋妖怪列表。

看起来 OP 的目标是制作这样的字典...

{
  nameA: { pass: [1, 2, ...], fail: [3, 4, ...] },
  nameB: { pass: [3, 4, ...], fail: [4, 5, ...] },
 ...
}

... 根据 api.

在输入文件中找到的通过和失败数组移动是或未学习的移动

由于我们不想冗余地调用api,并且由于输入文件中可能存在冗余移动,因此值得创建一个关联unique的中间结构 在输入中移动学习了它们的神奇宝贝,就像这样...

{ // key is a move id or name, value is an array of pokemon names
  1 : [ 'nameA', 'nameB', ... ],
  2 : [ 'nameC', 'nameD', ... ],
  ...
}

所以这就是我在代码中描述这个想法的方式(未编译或测试)...

async function get_learned_by_pokemon(move){
  const response = await axios.get(`https://pokeapi.co/api/v2/move/${move}`);
  return response.data.learned_by_pokemon.map(p => p.name);
}

async function move() {
  let db_data = JSON.parse(fs.readFileSync('pokes.json', 'utf8'));
  let allMoves = db_data.flat_map(p => p.moves);
  let uniqueMoves = [...new Set(allMoves)];

  // map move ids to pokemons who learned the moves  { moveId: [ 'pokeA', 'pokeB' ...], ...}
  let learnedBy = {}
  for (let move in uniqueMoves) {
    learnedBy[move] = await get_learned_by_pokemon(move);
  }

  // map pokemon names to a pair of arrays indicating if they have learned their moves (pass) or not (fail)
  let move_dict = db_data.reduce((acc, p) => {
    let name = p.name;
    let pass = p.moves.filter(move => learnedBy[move].includes(name));
    let fail = p.moves.filter(move => !learnedBy[move].includes(name));
    acc[name] = { pass, fail };
    return acc;
  }, {});

  return move_dict;
}