异步函数 await 不适用于备忘录?

Async function await is not working with memo?

我正在尝试制作异步 getData 的备忘录功能,但在将值添加到缓存中时,它没有等待响应并在值中添加未定义。

async function getData(searchKey){
    try{
    const response= await fetch(`http://openlibrary.org/search.json?title=${searchKey}`)
    const data = await response.json();
    showSuggestions(data , searchKey);
    }catch(err){
        console.log(err);
    }
}

const memoize = (fn) => {
    let cache = {};
    return async (...args) => {
      let n = args[0];  // just taking one argument here
      if (n in cache) {
        console.log('Fetching from cache', cache);
        return cache[n];
      }
      else {
        console.log('Calculating result');
        let result = await fn(n);
        cache[n] =  await result;
        return result;
      }
    }
  }
  // creating a memoized function for the 'add' pure function
  const memoizedgetData = memoize(getData);

您忘记 return getData 中的数据。 cache[n] = await result; 应该是 cache[n] = result;