I admit I haven't used cheerio before, but it looks like a good tool! The code looks decent - it appears that it will resolve and reject the promise when appropriate.
I see a couple places that can be simplified.
For example:
$('a').each(function(i, e) {
if ( $(e).attr('href').indexOf('reddit.com') === -1 &&
$(e).attr('href').indexOf('imgur.com') === -1 ) {
responseUrl = $(e).attr('href');
}
});
Instead of selecting all anchor tags and then checking to see if the href attribute doesn't contain one of two strings, those conditions could be used to only select such anchor tags. This can be simplified byachieved using the :not()
selector with an attribute selector *=
:
const links = $('a:not([href*="reddit.com"]):not([href*="imgur.com"])');
if (links.length) {
responseUrl = links.attr('href');
}
When I tested this with the sample data there was only one such anchor tag, but if there happen to be multiple, should responseUrl
be set to the first, last or other?
The only other simplification I see is this:
on('end', function() {
resolve(responseObject);
});
can be simplified to:
on('end', resolve.bind(null, responseObject));
using a partially applied function.