mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-10-08 23:25:15 +08:00
fix testurl
This commit is contained in:
26
lib/utils.ts
26
lib/utils.ts
@@ -1,4 +1,21 @@
|
||||
import { Platform } from "react-native";
|
||||
import { Platform } from 'react-native';
|
||||
|
||||
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 function logger(...args: any[]) {
|
||||
console.log('Pushy: ', ...args);
|
||||
@@ -17,16 +34,13 @@ 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)),
|
||||
]);
|
||||
|
||||
|
||||
export const testUrls = async (urls?: string[]) => {
|
||||
if (!urls?.length) {
|
||||
return null;
|
||||
}
|
||||
return Promise.race(urls.map(ping)).catch(() => null);
|
||||
return promiseAny(urls.map(ping)).catch(() => null);
|
||||
};
|
||||
|
Reference in New Issue
Block a user