不知道为什么我在获得对anyableAnagrams变量的不确定性,任何帮助将不胜感激!

const anagramica = require('anagramica');

const processBody = async (generatedString, arrayOfWords) => {
  const possibleAnagrams = await anagramica.all(generatedString, (err, response) => {
    if (err) {
      console.log(`Could not find possible anagrams : ${err}`);
    }
    else {
      console.log("1", response.all);
      /// [ Array ] ///
      const array = response.all;
      console.log("2", array);
      /// [ Array ] ///
      return array;
    }
  })
  console.log("3", possibleAnagrams)
  /// undefined ///

  return { possibleAnagrams, arrayOfWords }
}

最佳答案

看来anagramica没有返回Promise。如果您想使用异步/等待,请考虑将其转换为Promise。

const anagramica = require('anagramica');

const anagramicaPromise = generatedString => new Promise((res, rej) => {
  anagramica.all(generatedString, (err, response) => {
    if (err) {
      return rej(err);
    }
    res(response);
  }
});

const processBody = async (generatedString, arrayOfWords) => {
  try {
    const possibleAnagrams = await anagramicaPromise(generatedString);
    console.log("3", possibleAnagrams);
    return { possibleAnagrams, arrayOfWords }
  } catch(err) {
    console.log(`Could not find possible anagrams : ${err}`);
  }
}

07-22 23:00