1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-10-29 20:43:09 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

fix testurl

This commit is contained in:
sunny.luo
2024-11-12 23:59:16 +08:00
parent 45dd8b2974
commit d2f23ada25
2 changed files with 25 additions and 12 deletions

View File

@@ -4,7 +4,25 @@ export function log(...args: any[]) {
console.log('pushy: ', ...args);
}
const noop = () => {};
export function promiseAny<T>(promises: Promise<T>[]) {
return new Promise<T>((resolve, reject) => {
let count = 0;
promises.forEach(promise => {
Promise.resolve(promise)
.then(resolve)
.catch(() => {
count++;
if (count === promises.length) {
reject(new Error('All promises were rejected'));
}
});
});
});
}
export const emptyObj = {};
export const noop = () => {};
class EmptyModule {
constructor() {
return new Proxy(this, {
@@ -23,9 +41,7 @@ const ping =
Promise.race([
fetch(url, {
method: 'HEAD',
})
.then(({ status }) => (status === 200 ? url : null))
.catch(() => null),
}).then(({ status }) => (status === 200 ? url : null)),
new Promise(r => setTimeout(() => r(null), 2000)),
]);
@@ -44,5 +60,5 @@ export const testUrls = async (urls?: string[]) => {
if (await canUseGoogle) {
return urls[0];
}
return Promise.race(urls.map(ping)).catch(() => null);
return promiseAny(urls.map(ping)).catch(() => null);
};