question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Refactoring of a Promise.all() in opensource.ts

See original GitHub issue

There’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:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
stemountcommented, Dec 15, 2020

@BOLT04 EXACTLY!!! Open Source works! Other people can improve on your code suggestions!

What a revelation!!

Only trolling. Absolutely, this looks really neat now. ^^^^^

1reaction
eddiejaoudecommented, Feb 17, 2021

I will share this in Discord as it is a good first issue

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found