Skip to content

Commit 0142b04

Browse files
author
Peter Bengtsson
authored
use Promise.all() on iterator of async functions (#23520)
1 parent 5d5e280 commit 0142b04

File tree

1 file changed

+61
-55
lines changed

1 file changed

+61
-55
lines changed

script/rendered-content-link-checker.mjs

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -186,75 +186,81 @@ function getPages(pageList, languages, filters, files, max) {
186186
}
187187

188188
async function processPage(page, pageMap, redirects, opts) {
189-
const { bail, level, verboseUrl, checkAnchors, checkImages } = opts
190-
const allFlaws = []
191-
192-
for (const permalink of page.permalinks) {
193-
const html = await renderInnerHTML(page, permalink)
194-
const $ = cheerio.load(html)
195-
const flaws = []
196-
$('a[href]').each((i, link) => {
197-
const { href } = link.attribs
198-
199-
// The global cache can't be used for anchor links because they
200-
// depend on each page it renders
201-
if (!href.startsWith('#')) {
202-
if (globalHrefCheckCache.has(href)) {
203-
globalCacheHitCount++
204-
return globalHrefCheckCache.get(href)
205-
}
206-
globalCacheMissCount++
207-
}
189+
const { bail, verboseUrl } = opts
208190

209-
const flaw = checkHrefLink(href, $, redirects, pageMap, checkAnchors)
191+
const allFlawsEach = await Promise.all(
192+
page.permalinks.map((permalink) => processPermalink(permalink, page, pageMap, redirects, opts))
193+
)
210194

211-
// Again if it's *not* an anchor link, we can use the cache.
212-
if (!href.startsWith('#')) {
213-
globalHrefCheckCache.set(href, flaw)
214-
}
195+
const allFlaws = allFlawsEach.flat()
215196

216-
if (flaw) {
217-
if (level === 'critical' && !flaw.CRITICAL) {
218-
return
219-
}
220-
const text = $(link).text()
221-
flaws.push({ permalink, page, href, flaw, text })
222-
}
223-
})
197+
if (bail && allFlaws.length > 0) {
198+
printFlaws(allFlaws, verboseUrl)
199+
process.exit(1)
200+
}
224201

225-
if (checkImages) {
226-
$('img[src]').each((i, img) => {
227-
const { src } = img.attribs
202+
printFlaws(allFlaws, verboseUrl)
228203

229-
if (globalImageSrcCheckCache.has(src)) {
230-
globalCacheHitCount++
231-
return globalImageSrcCheckCache.get(src)
232-
}
204+
return allFlaws
205+
}
233206

234-
const flaw = checkImageSrc(src, $)
207+
async function processPermalink(permalink, page, pageMap, redirects, opts) {
208+
const { level, checkAnchors, checkImages } = opts
209+
const html = await renderInnerHTML(page, permalink)
210+
const $ = cheerio.load(html)
211+
const flaws = []
212+
$('a[href]').each((i, link) => {
213+
const { href } = link.attribs
214+
215+
// The global cache can't be used for anchor links because they
216+
// depend on each page it renders
217+
if (!href.startsWith('#')) {
218+
if (globalHrefCheckCache.has(href)) {
219+
globalCacheHitCount++
220+
return globalHrefCheckCache.get(href)
221+
}
222+
globalCacheMissCount++
223+
}
235224

236-
globalImageSrcCheckCache.set(src, flaw)
225+
const flaw = checkHrefLink(href, $, redirects, pageMap, checkAnchors)
237226

238-
if (flaw) {
239-
if (level === 'critical' && !flaw.CRITICAL) {
240-
return
241-
}
242-
flaws.push({ permalink, page, src, flaw })
243-
}
244-
})
227+
// Again if it's *not* an anchor link, we can use the cache.
228+
if (!href.startsWith('#')) {
229+
globalHrefCheckCache.set(href, flaw)
245230
}
246231

247-
if (bail && flaws.length > 0) {
248-
printFlaws(flaws, verboseUrl)
249-
process.exit(1)
232+
if (flaw) {
233+
if (level === 'critical' && !flaw.CRITICAL) {
234+
return
235+
}
236+
const text = $(link).text()
237+
flaws.push({ permalink, page, href, flaw, text })
250238
}
239+
})
251240

252-
allFlaws.push(...flaws)
253-
}
241+
if (checkImages) {
242+
$('img[src]').each((i, img) => {
243+
const { src } = img.attribs
254244

255-
printFlaws(allFlaws, verboseUrl)
245+
if (globalImageSrcCheckCache.has(src)) {
246+
globalCacheHitCount++
247+
return globalImageSrcCheckCache.get(src)
248+
}
256249

257-
return allFlaws
250+
const flaw = checkImageSrc(src, $)
251+
252+
globalImageSrcCheckCache.set(src, flaw)
253+
254+
if (flaw) {
255+
if (level === 'critical' && !flaw.CRITICAL) {
256+
return
257+
}
258+
flaws.push({ permalink, page, src, flaw })
259+
}
260+
})
261+
}
262+
263+
return flaws
258264
}
259265

260266
function printFlaws(flaws, verboseUrl = null) {

0 commit comments

Comments
 (0)