From f1d931107c23d418b5ffbaba68f53385ccd4b1fe Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Tue, 12 Oct 2021 19:05:50 +0800 Subject: [PATCH] retry fetching group info --- src/App.svelte | 19 ++++++++++++++----- src/util.js | 8 ++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 src/util.js diff --git a/src/App.svelte b/src/App.svelte index 2251362..f98a6b9 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -2,6 +2,7 @@ import { onMount, setContext } from 'svelte' import Message from './Message.svelte' import Name from './Name.svelte' + import { sleep } from './util.js' const LUOXU_URL = 'https://lab.lilydjwg.me/luoxu' let groups = [] @@ -30,11 +31,19 @@ onMount(async () => { do_hash_search() - const res = await fetch(`${LUOXU_URL}/groups`) - groups = (await res.json()).groups - need_update_title = true - if(!group) { - group = '' + while(true) { + try{ + const res = await fetch(`${LUOXU_URL}/groups`) + groups = (await res.json()).groups + need_update_title = true + if(!group) { + group = '' + } + break + }catch(e){ + console.error('failed to fetch group info, will retry', e) + await sleep(1000) + } } }) diff --git a/src/util.js b/src/util.js new file mode 100644 index 0000000..0cbbb33 --- /dev/null +++ b/src/util.js @@ -0,0 +1,8 @@ +'use strict' + +export function sleep(ms) { + const p = new Promise((resolve, reject) => { + setTimeout(resolve, ms) + }) + return p +}