async function main () { let url = ' ... ' while (url) { const resp = await fetch(url) const json = await resp.json() url = json.url } }
function awaitOnline () { return new Promise(resolve => { // Если клиент уже online — немедленно возвращаем результат if (navigator.onLine) { resolve() return } // Регистрируем обработчик и решаем промис как только клиет будет online window.addEventListener( 'online', () => resolve(), {once: true} // Автоматически удаляем обработчик после первого события ) }) }
async function main () { let url = ' ... ' while (url) { await awaitOnline() // Выполнение приостановится в этом месте до момента возобновления сети const resp = await fetch(url) const json = await resp.json() url = json.url } }
Source: https://habr.com/ru/post/436426/