zach.codes

Share this post

User's avatar
zach.codes
Recursive Promises in Node
Copy link
Facebook
Email
Notes
More

Recursive Promises in Node

Zach Silveira
Apr 16, 2017
∙ Paid

Share this post

User's avatar
zach.codes
Recursive Promises in Node
Copy link
Facebook
Email
Notes
More
Share

Recursive functions are extremely useful for many things in JavaScript. In React, you might want to traverse through all the children of a higher order component, even nested ones. There's many valid use-cases for these functions.

Last week I realized that I needed to recursively call a promise and eventually resolve at an unknown point. Why? In this case, I had to build a website scraper... I know, it's the best (not). I needed a way to keep clicking the next page link on a website until it no longer existed, and then resolve all URL's found on those pages.

Initially I was stuck because I couldn't find much help online. After a couple times of trial and error, I was overthinking it! Let's jump into the code:

const renderURLs = (site, url, existingData = []) => 
  new Promise(async resolve => {
    let response = await renderPage(site, url);
    let nextURL = getNextUrl(response.$);

    if (!nextURL) return resolve([...response.data, ...existingData]);
    else renderURLs(site, nextURL,…

Keep reading with a 7-day free trial

Subscribe to zach.codes to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 Zach Silveira
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share

Copy link
Facebook
Email
Notes
More