Refactoring of a Promise.all() in opensource.ts
See original GitHub issueThere’s an improvement available here (ideal for first-timers with Promise)
https://github.com/EddieJaoudeCommunity/EddieBot/blob/develop/src/commandHandlers/tips/opensource.ts
/**
* Set the given embed message with a curated list of tips and resources that help members of the community to
* contribute to open-source software (OSS).
*/
export const createTip = async (embed: MessageEmbed) => {
const tips = await Promise.all(
config.TIPS.tips.map((file) => readMarkdown(file))
);
const resources = await Promise.all(
config.TIPS.resources.map((file) => readMarkdown(file))
);
tips.forEach((tip) => embed.addField('Tips :bulb:', tip));
resources.forEach((resource) =>
embed.addField('Links and Resources :link:', resource)
);
It is possible to do something like:
// Use ES6 deconstructing.
// This is actually totally untested and might not actually work, however Promise.all() can work in harmony.
const { tips, resources } = await Promise.all([
config.TIPS.tips.map((file) => readMarkdown(file)),
config.TIPS.resources.map((file) => readMarkdown(file))
]);
Issue Analytics
- State:
- Created 3 years ago
- Reactions:5
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Proposal: Code Fix -- Convert Promise Handlers to Async and ...
This proposal allows the Typescript language service to automatically refactor code that uses Promise methods such as .then() or .catch() to ...
Read more >Refactoring by Breaking Functions Apart: a TypeScript ... - Auth0
Learn how to refactor a complex TypeScript function by breaking it into a composition of simpler ones.
Read more >Promise.all() - JavaScript - MDN Web Docs
The Promise.all() method takes an iterable of promises as input and returns a single Promise . This returned promise fulfills when all of ......
Read more >Beware of Promise.all - DEV Community
In Javascript, Promise.all lets you execute a bunch of Promises in parallel and get an array of results back.
Read more >Refactoring Promise Chains w/Async-Await | by Brian Lego
So I resolved to finally make use of the async-await keywords and refactor all of my existing code to read more synchronously. Let's...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@BOLT04 EXACTLY!!! Open Source works! Other people can improve on your code suggestions!
What a revelation!!
Only trolling. Absolutely, this looks really neat now. ^^^^^
I will share this in Discord as it is a
good first issue