2 ms·
Agreed, thought there is no need to complicate it further by returning objects with status and value fields. The requirements here seem a bit rare (ignore fail
by ricardobeat 4y ago
Agreed, thought there is no need to complicate it further by returning objects with status and value fields.
The requirements here seem a bit rare (ignore failures individually, machine gun requests, render incrementally), but the ideal solution is actually glossed over in the post, if you consider that you'll probably want to log those request errors in production:
async function showChapters(chapterURLs) {
const requests = chapterURLs.map(url => {
return fetch(url).then(r => r.json()).catch((err) => {
MyMonitoring.report(err)
})
})
for await (const data of requests) {
appendChapter(data);
}
}
In this scenario, and the post also fails to mention this, appendChapter() needs to deal with empty data as the .catch() handler is resolving to undefined.
This could be easily abstracted into a 'fetchAll' function.
There is absolutely nothing "blunt" or wrong about it. The engine warned us about unhandled promises, we handle them, everybody is happy!
- jaffathecake 4y agoBut now some of your requests resolve with undefined, rather than correctly reject (it's exceptional behaviour). appendChapter(undefined) doesn't seem right to me. Feels like you'll have to do something like if (data === undefined) throw… which kinda shows how hacky this is.
- ricardobeat 4y agoHi Jake! That is exactly what the code in the article does, isn't it? promise.catch(() => {}) It does follow up with "this doesn't change the promises other than marking them as 'handled'. They're still rejected promises" but that doesn't seem right - they become fulfilled promises with a value of undefined, and those values will definitely show up in the for loop later. Is there something I'm missing with the nested promises? My suggestion was in the spirit of keeping this behaviour the same, except I think explicitly adding the catch handler to fetch() is the right way to do it vs an auxiliary function to 'silence' them.
- jaffathecake 4y ago> they become fulfilled promises with a value of undefined No they don't, which is why I made that clarification in the post :D const promise1 = Promise.reject(Error("wow")); // promise1 is rejected, and unhandled const promise2 = promise1.catch(() => {}); // promise2 will be fulfilled, and is unhandled // promise1 is still rejected, but now handled