

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Concurrency
incomplete
2: Max Pages
incomplete
This lesson's interactive features are locked, please to keep using them
Your web crawler works – but it's crawling pages one at a time. It would take us a really long time to crawl a large website. Let's make it faster using Promise-based concurrency. This is another long step, but don't get discouraged!
We will be using the p-limit package to help us add concurrency to our project.
npm install p-limit
baseURL (the starting URL)pages (our Record of page visit counts)limit (a function created with pLimit(maxConcurrency))private addPageVisit(normalizedURL: string): boolean
private async getHTML(currentURL: string): Promise<string>
limit function and return the result:
return await this.limit(async () => {
// same fetch logic and error handling as before
// return res.text() here
});
Wrapping our getHTML logic inside the limit function, gives us control over how many concurrent fetches run at once. This can be adjusted by changing the value of maxConcurrency. Keeping this low helps us avoid hammering the target server with too many requests.
private async crawlPage(currentURL: string): Promise<void>
addPageVisit method, if it is not a new page return earlygetHTML methodthis.crawlPage(nextURL)Promise.all() to await all the concurrent crawl promisesConcurrentCrawler instancecrawl() methodaddPageVisit method returns a boolean: to indicate if it's the first time we've seen the page.Promise.all() to handle multiple concurrent requests efficiently