首页 > 其他分享 >资源探活

资源探活

时间:2023-01-25 01:24:31浏览次数:42  
标签:return url Promise reject export statusCode 资源 探活

export function probeSourceLive(url: string): Promise<string> {
    return new Promise((resolve, reject) => {
        uni.request({
            url,
            method: 'HEAD',
            success: (res) => {
                const { statusCode } = res;
                if (statusCode === 200 || String(statusCode).startsWith('3')) {
                    resolve(url);
                } else {
                    reject(url);
                }
            },
            fail: () => {
                reject(url);
            },
        });
    });
}

export function probeFirstLive(urls: Array<string>): Promise<string> {
    const array: Array<Promise<string>> = urls.map((item) => {
        return probeSourceLive(item);
    });
    return Promise.any(array);
}

 

标签:return,url,Promise,reject,export,statusCode,资源,探活
From: https://www.cnblogs.com/zhenjianyu/p/17058121.html

相关文章