From 48ec6bec9f145c02b59eac5af00d08e29acf522c Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Thu, 30 Aug 2018 14:44:28 +0100 Subject: [PATCH] googlescholar: fix linting violation: no-await-in-loop (#590) #588 --- routes/google/scholar.js | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/routes/google/scholar.js b/routes/google/scholar.js index 701188849..183863f2a 100644 --- a/routes/google/scholar.js +++ b/routes/google/scholar.js @@ -22,29 +22,27 @@ module.exports = async (ctx) => { }); const $ = cheerio.load(response.data); - const list = $('#gs_res_ccl_mid .gs_r.gs_or.gs_scl .gs_ri'); + const list = $('#gs_res_ccl_mid .gs_r.gs_or.gs_scl .gs_ri').get(); - const out = []; - - for (let i = 0; i < list.length; i++) { - const $ = cheerio.load(list[i]); - const itemUrl = $('h3 a').attr('href'); - const cache = await ctx.cache.get(itemUrl); - if (cache) { - out.push(JSON.parse(cache)); - continue; - } - const single = { - title: $('h3 a').text(), - author: $('.gs_a').text(), - description: $('.gs_rs').text(), - link: itemUrl, - guid: itemUrl, - }; - - out.push(single); - } + const out = await Promise.all( + list.map(async (item) => { + const $ = cheerio.load(item); + const itemUrl = $('h3 a').attr('href'); + const cache = await ctx.cache.get(itemUrl); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + const single = { + title: $('h3 a').text(), + author: $('.gs_a').text(), + description: $('.gs_rs').text(), + link: itemUrl, + guid: itemUrl, + }; + return Promise.resolve(single); + }) + ); ctx.state.data = { title: `Google Scholar Monitor: ${query}`, link: url,