[英]How can I manage to make diffrent request after first request with failed status
I try to fetch some object, but the problem is that I need to check first if there ist any object on cache endpoint, if not I should do normal fetching from regular endpoint. 我尝试获取一些对象,但是问题是我需要首先检查缓存端点上是否有任何对象,否则,我应该从常规端点进行正常的获取。
So far I only managed to do fetching from: 到目前为止,我只能从以下位置进行获取:
Any attempts to mix both methods ended in failure :( 任何将两种方法混合使用的尝试均以失败告终:(
How can I mix this two methods? 如何将这两种方法混合使用?
const getCache = async () => {
try {
const apiCall = await fetch(fetchCacheEndpoint)
const data = await apiCall.json()
return data
} catch (e) {
console.log(e);
}
}
const pageOne = getCache().then((result) => {
const convertedOBj = result.doSomeSeriousStuff()
this.setState({
desiredObj: convertedOBj
})
})
I expected to do something like this 我希望做这样的事情
const getCache = async () => {
try {
const apiCall = await fetch(fetchCacheEndpoint)
const data = await apiCall.json()
return data
} catch (e) {
console.log(e);
}
}
let convertedOBj
const pageOne = getCache().then((result) => {
if ((result === undefined) || (!result) || (result && !result.length > 0)) {
const makeRegularFetch = async () => {
const regularFetch = await fetch(regularEndPoint)
const data = await regularFetch.json()
}
const pageTwo = makeRegularFetch ().then((result) => {
convertedOBj = result.doSomeSeriousStuff()
this.setState({
desiredObj: convertedOBj
})
})
} else {
convertedOBj = result.doSomeSeriousStuff()
this.setState({
desiredObj: convertedOBj
})
}
})
After first fetch (getCache) is failed there is another fetch (makeRegularFetch) to second endpoint which is always fine, but only in the case when first(getCache) return empty object or just any kind of error How can I handle this kind of action? 在第一个提取(getCache)失败后,有另一个对第二个端点的提取(makeRegularFetch)总是可以的,但是仅在first(getCache)返回空对象或只是任何类型的错误的情况下,我该如何处理此类操作?
From what I can see in your second part of your code, you never execute your pageOne
function. 从我在代码的第二部分中看到的,您永远不会执行
pageOne
函数。
Try pageOne()
after your definition. 在定义后尝试
pageOne()
。
However I made a fiddle on stackblitz for your case: https://stackblitz.com/edit/js-eufm8h 但是我为您的情况在stackblitz上做了一个小提琴: https ://stackblitz.com/edit/js-eufm8h
If you don't understand something, feel free to ask. 如果您听不懂,请随时提出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.