typescript - 检查承诺的类型(使用 void 与否?)
问题描述
对于返回 Promise 的通用请求函数。你应该如何处理捕获?
感觉好像我应该有'无效'。但是随后拾取这些值会产生错误。
const request = async function<T>(url: string, context = ''): Promise<T | void> {
return fetch(url)
.then(res => res.json() as Promise<T>)
.catch(err => {
// this part is void?
console.warn('Failed to load fetch.');
});
};
const params = await request<{ relations: { name: string; }[] } | void>('https://www.example.com');
params.relations.filter(( relation ) => {
console.log(relation);
});
// Error:
// Property 'relations' does not exist on type 'void | { relations: { name: string; }[] }'.
// Property 'relations' does not exist on type 'void'.
解决方案
由于request
将拒绝转换为履行undefined
,因此在调用它时需要一个守卫,例如:
const params = await request<{ relations: { name: string; }[] } | void>('https://www.example.com');
if (!params) {
// It didn't work
} else {
params.relations.filter(( relation ) => {
console.log(relation);
});
}
但是,通常最好不要将拒绝转换为履行,以便调用者可以用try
/catch
或让拒绝传播等来区分它们。这对于async
函数特别有用(鉴于您使用 ,您似乎正在使用它await
,因为拒绝传播就像扔的东西throw
一样。
所以我会删除那部分request
,这也可以让你删除| void
:
const request = async function<T>(url: string, context = ''): Promise<T> {
return fetch(url)
.then(res => res.json() as Promise<T>);
};
那么使用的时候就不需要守卫了,| void
调用的时候也不要在类型参数中使用:
const params = await request<{ relations: { name: string; }[] }>('https://www.example.com');
params.relations.filter(( relation ) => {
console.log(relation);
});
旁注:该代码正在成为fetch
API 的猎物:fetch
仅拒绝其对网络错误的承诺,而不是 HTTP 错误。您必须自己检查 HTTP 是否成功:
const request = async function<T>(url: string, context = ''): Promise<T> {
return fetch(url)
.then(res => {
if (!res.ok) {
throw new Error("HTTP error " + res.status);
}
return res.json() as Promise<T>;
});
};
推荐阅读
- docker - Registering models from Databricks to Azure ML and save Azure ML image into provided ACR(Non Default ACR of AML Workspace)
- javascript - Thymeleaf - 如何交互和重新加载 Javascript?
- python - 使用 Python 请求模块下载的文件引发无效文件错误
- java - 如果 pg_restore 正在进行,我们会在应用程序中遇到任何运行时问题吗?
- javascript - 如何找到 React 函数的作用域?
- javascript - 为什么我的代码在文本字段中给出的时间没有重新加载
- mysql - 如何让关注用户发布具有可见性条件的提要?
- python - 基于 GPIO 输入和时间的 Python rpi GPIO 输出控制
- openoffice-calc - OpenOffice Calc 使用许多 cels 将二进制代码转换为十进制
- android - 单元测试 ViewModel 类