![](/img/trans.png)
[英]How to handle returned different http status codes separately in Fetch Api?
[英]Handle different variables for fetch api
我如何用一個 function 填充來自不同提取的不同變量?
填充后我想將緩存導出到另一個 js 文件,但我不知道該怎么做:
var Acache = {}, Bcache = {} ;
fetch(APIcall1).then(
res => {
handlecache(res, Acache);
}
);
fetch(APIcall2).then(
res => {
handlecache(res, Bcache);
}
);
function handlecache(res, cache) {
res.json()
.then((j) => {
cache(!! not working!!) = j;
console.log(acache, bcache);
})
}
復制object並引用handlecache()
中的變量有問題
這應該如您所料工作:
const Cache = {
A: {},
B: {}
};
fetch(APIcall1).then(res => {
handlecache(res, 'A'); // Pass the obj key instead of whole object
});
fetch(APIcall2).then(res => {
handlecache(res, 'B');
});
function handlecache(res, key) {
res.json().then(j => {
// Copy response data and save to the end of Cache using spread syntax
Cache[key] = { ...Cache[target], ...j };
console.log(Cache);
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.