如何仅在Promise中返回解析类型?

let companyInfo: PublicCompanyAPIResponseType;
companyInfo = await get<PublicCompanyAPIResponseType>({
    url: getcompanyDataURL,}).catch(res => {
    responseStatus = res.status;
});

当我将companyInfo变量分配给该函数时

export async function get<T>({ url,headers }: ApiConnectSet): Promise<T> {
return new Promise((resolve,reject) => {
    fetch(url,{
        method: 'GET',credentials: 'same-origin',headers: headers,})
        .then(async res => {
            if (res.ok) {
                return resolve((await res.json()) as Promise<T>);
            } else if (res.status === 401) {
                const redirectPath = window.location.pathname;
                window.location.href =
                    '/login?redirectPath=' + redirectPath;
            } else {
                reject(res);
            }
        })
        .catch(error => {
            reject(error);
        });
});

}

Visual Studio代码显示此错误

如何仅在Promise中返回解析类型?

我的get函数如何只能返回PublicCompanyAPIResponseType

beibei0524 回答:如何仅在Promise中返回解析类型?

尝试一下,如果可行

let companyInfo: PublicCompanyAPIResponseType;
try {
  companyInfo = await get<PublicCompanyAPIResponseType>({
    url: getCompanyDataURL,})
} catch(err => {
    // get the status from error object and assign it to response status
    responseStatus = // your status code
});
,

您是否尝试过在return语句中添加 <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-feature android:name="android.hardware.camera" />

Promise<T>
本文链接:https://www.f2er.com/2658804.html

大家都在问