资源探活

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);
}

 

posted @ 2023-01-25 01:14  671_MrSix  阅读(15)  评论(0编辑  收藏  举报