Skip to content

Commit

Permalink
Refactoring searchHotTrends.js to use async/await function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Pacheco95 committed Mar 8, 2019
1 parent 6645b7a commit 579fa01
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
22 changes: 9 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
const readline = require('readline-sync');
const trends = require('./searchHotTrends');

function start() {
async function start() {
const searchFields = {};

askAndReturnSearchTerm()
.then(term => {
searchFields.searchTerm = term;
searchFields.prefix = askAndReturnPrefix();
console.log(searchFields);
});
searchFields.searchTerm = await askAndReturnSearchTerm();
searchFields.prefix = askAndReturnPrefix();

function askAndReturnSearchTerm() {
console.log(searchFields);

async function askAndReturnSearchTerm() {
let typedTerm = readline.question('Type a search term or press <Enter> to get a list of hot terms: ');
if (typedTerm)
return new Promise(resolve => resolve(typedTerm));
return typedTerm;
else {
console.log('Auto searching for hot terms online...');
return trends.searchHotTrends()
.then(function (hotTerms) {
return getUserOptionInput(hotTerms, 'Choose on term: ');
});
const hotTerms = await trends.searchHotTrends(9);
return getUserOptionInput(hotTerms, 'Choose one term: ');
}
}

Expand Down
14 changes: 8 additions & 6 deletions searchHotTrends.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
const googleTrends = require('google-trends-api');

module.exports = {
searchHotTrends: function () {
let trendsSettings = {
searchHotTrends: function (count) {
const trendsSettings = {
trendDate: new Date(),
geo: 'BR',
hl: "pt-BR"
};

return googleTrends.realTimeTrends(trendsSettings)
const result = googleTrends.realTimeTrends(trendsSettings)
.then(results => {
let hotTrendStories = JSON.parse(results)['storySummaries']['trendingStories'];
const hotTrendStories = JSON.parse(results)['storySummaries']['trendingStories'];
let hotTrends = [];
hotTrendStories.forEach((story) => hotTrends = hotTrends.concat(story['entityNames']));
return hotTrends.slice(0, 9);
return hotTrends.slice(0, count);
})
.catch(error => {
console.error('An error occurs: ', error);
return [];
});

return result;
}
};
};

0 comments on commit 579fa01

Please sign in to comment.