我想出了这种进行网络操作的方式,ApolloClient
但问题是代码看起来非常难看且难以阅读,考虑到我必须编写数十个这样的查询,它变得令人厌烦且无法维护。
我在 Apollo 文档或配置超时的实际代码中没有找到任何内容。
let query = gql`
query ... {
}`;
let x = 0;
let timer = setTimeout(() => {
if (x === 0) {
console.log('error');
}
x = 1;
}, 3000);
ApolloClient.query({ query }).then(({data}) => {
clearTimeout(timer);
if (x === 0) {
if (data.result) {
console.log(data.result)
} else {
console.log('error');
}
}
}).catch((error) => {
clearTimeout(timer);
console.log('error')
});
有没有更好的方法可以用更少和更简单的代码实现相同的结果?