For the Parallel API Calls you don't even need async/await, you can just:
async
await
const getData = () => Promise.all([fetch(), fetch(), fetch(), fetch()]) .then(([first, second, third, fourth]) => { /* ... */ }) .catch(console.error);
For the Using Objects instead of Switch for event binding you forgot about the default handler, you can use ?? for it:
??
const eventHandler = { success: handleSuccess, error: handleError, pending: handlePending, }; const handleEvent = event => eventHandler[event] ?? handleDefault;
Also in the honorable mentions "Use async/await instead of promise callback chain". Why tho? If you do things correctly, the code stays clean:
try { const response = await fetch(ENDPOINT); const data = await response.json(); console.log(data); } catch (error) { console.error(error); } // vs fetch(ENDPOINT) .then(response => response.json()) .then(console.log) .catch(console.error);
I'm not saying async/await is not useful, I'm just saying that you don't need them always, and if you do things correctly, your code stays simple without that syntax sugar.
Cheers!
The last point is heavily disputed and there are a lot of opinionated people on either side.
My position is: use what fits your case best. You can also mix await promise emitter().catch(...) for extra conciseness.
await promise emitter().catch(...)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
For the Parallel API Calls you don't even need
async/await, you can just:For the Using Objects instead of Switch for event binding you forgot about the default handler, you can use
??for it:Also in the honorable mentions "Use
async/awaitinstead of promise callback chain". Why tho? If you do things correctly, the code stays clean:I'm not saying
async/awaitis not useful, I'm just saying that you don't need them always, and if you do things correctly, your code stays simple without that syntax sugar.Cheers!
The last point is heavily disputed and there are a lot of opinionated people on either side.
My position is: use what fits your case best. You can also mix
await promise emitter().catch(...)for extra conciseness.