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
then
methods toDataService.GetLastChackDate()
and then later on to thepostFeedPromise
variable, albeit the same value. Doing one instead of both might provide some readability. Should the chain wait for allsendMsg
promises to be resolved before callingDataService.UpdateLastChackDate()
? Because that is not yet the case. \$\endgroup\$