Skip to content

Instantly share code, notes, and snippets.

@jakearchibald
Last active February 1, 2023 17:46
Embed
What would you like to do?
function preventUnhandledRejections(...promises) {
for (const promise of promises) promise.catch(() => {});
}
async function showChapters(chapterURLs, { signal: outerSignal }) {
outerSignal?.throwIfAborted();
const controller = new AbortController();
outerSignal?.addEventListener('abort', () => controller.abort());
const chapterPromises = chapterURLs.map(async (url) => {
const response = await fetch(url, { signal: controller.signal });
let data;
try {
data = await response.json();
} catch (err) {
throw Error(`Failed to parse ${url} as JSON: ${response.statusCode} ${response.statusText}`);
}
if (data.error) throw Error(data.error);
return data;
});
// Avoid unhandled rejections leaking out of this function.
// The subsequent `for await` handles all the relevant promises.
preventUnhandledRejections(...chapterPromises);
try {
for await (const chapterData of chapterPromises) {
appendChapter(chapterData);
}
} finally {
// Abort any remaining fetches
controller.abort();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment