问题描述
我正在尝试创建 async getData 的备忘录功能,但是在将值添加到缓存中时,它没有等待响应并在值中添加 undefined。
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);
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)