2
\$\begingroup\$

I'm new to using JS for anything other than DOM manipulation, so I'm not sure if the following code is taking full advantage of the Async concept in JS.

This is an example of a function I'm writing in my Discord.js bot project. The function checks for new RSS feed items from a given URL and then calls another function that posts the new items as Discord messages:

const postLatestFeed = () => {
   console.log("Checking for new feed...");
   let postFeedPromise = DataService.GetLastChackDate() // GetLastChackDate() is a promise
       .then((lastCheckDate) => {
          console.log("Last check date is:");
          console.log(lastCheckDate);
          return RssService.GetRssItems(RSS_FEED_URL, lastCheckDate);
       })
       .then((rssItems) => {
          console.log("Found " + rssItems.length + " new feed items");
          rssItems.forEach((item) => {
            let msg = DiscordMsgHelper.ParseGameFeedMsg(item, FEED_WATCHING_ROLE);
            sendMsg(msg, FEED_CHANNEL_ID); // sendMsg is also a promise void
          });
       });
    postFeedPromise.then((_) => DataService.UpdateLastChackDate()); //forked, UpdateLastChackDate() is a promise
    postFeedPromise.catch((e) => console.error(e));
};
// and this is how I'm calling postLatestFeed
postLatestFeed(); // as I'm not currently expecting any return or status/report just catching

So do I know what I'm doing here? The code is producing the expected results. I just want to make sure that I'm doing it correctly. Sorry if you don't like the naming, I like capitalizing exported public methods names :3

\$\endgroup\$
8
  • \$\begingroup\$ Welcome to Code Review. Does the code produce the correct output? \$\endgroup\$
    – Mast
    Commented Oct 3, 2021 at 12:35
  • \$\begingroup\$ yes it is working as expected \$\endgroup\$
    – RaVeN
    Commented Oct 3, 2021 at 12:41
  • 1
    \$\begingroup\$ Great! The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How to Ask for examples, and revise the title accordingly. \$\endgroup\$
    – Mast
    Commented Oct 3, 2021 at 12:43
  • 1
    \$\begingroup\$ The syntax looks right and you're using it properly. However it might strike as visually odd to chain the first two then methods to DataService.GetLastChackDate() and then later on to the postFeedPromise variable, albeit the same value. Doing one instead of both might provide some readability. Should the chain wait for all sendMsg promises to be resolved before calling DataService.UpdateLastChackDate()? Because that is not yet the case. \$\endgroup\$ Commented Oct 3, 2021 at 14:52
  • 1
    \$\begingroup\$ The whole point was not to state your concerns about the code in the question title. I've modified it to the best of my understanding. \$\endgroup\$
    – Mast
    Commented Oct 3, 2021 at 16:28

1 Answer 1

1
\$\begingroup\$

The way you wrote your call to DataService.UpdateLastChackDate() makes it appear like there was some coding error that happened. Arrow functions take an expression-only body as its return value. Writing it this way makes it seem like we're awaiting for DataService.UpdateLastChackDate() when in fact we don't really care.

// The resulting promise will not resolve until UpdateLastChackDate()
// resolves. But the promise is assigned to nothing. Did we mean to
// chain? Did we forget to assign the promise somewhere?
postFeedPromise.then((_) => DataService.UpdateLastChackDate())

I would write this to explicitly say that I don't care about the result by placing braces around it.

// No return means promise resolves immediately with an undefined.
postFeedPromise.then((_) => { DataService.UpdateLastChackDate() })

Alternatively, DataService.UpdateLastChackDate() could just be called right after the forEach() since we're not awaiting anything from that then() either.

And then, we could also write this in async/await form for better readability:

const postLatestFeed = async () => {

  try {
    console.log('Checking for new feed...')
  
    const lastCheckDate = await DataService.GetLastChackDate()

    console.log('Last check date is:', lastCheckDate)

    const rssItems = await RssService.GetRssItems(RSS_FEED_URL, lastCheckDate)

    console.log(`Found ${rssItems.length} new feed items`)

    rssItems.forEach((item) => {
      let msg = DiscordMsgHelper.ParseGameFeedMsg(item, FEED_WATCHING_ROLE)
      sendMsg(msg, FEED_CHANNEL_ID)
    })

    DataService.UpdateLastChackDate()
  } catch (e) {
    console.warn(e)
  }
}

postLatestFeed()
\$\endgroup\$
2
  • \$\begingroup\$ Alright I got you.. yk that line did feel weird tbh.. thanks a lot man appreciate it \$\endgroup\$
    – RaVeN
    Commented Oct 4, 2021 at 17:18
  • \$\begingroup\$ Anything else you found in the example? or should I just mark it as solved? \$\endgroup\$
    – RaVeN
    Commented Oct 4, 2021 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.