-1

I'm trying to crawl movie data from TMDB website. I finished my code with pure javascript, but I want to change the code into functional programming style by using ramda.js.

I attached my code below. I want to get rid of for-loop (if it is possible) and use R.pipe function.

(async () => {
  for (let i = 0; i < 1000; i++) {
    (() => {
      setTimeout(async () => {
        let year = startYr + Math.floor(i / 5);
        await request.get(path(year, i % 5 + 1), async (err, res, data) => {
          const $ = cheerio.load(data);
          let list = $('.results_poster_card .poster.card .info .flex a');
          _.forEach(list, (element, index) => {
            listJSON.push({
              MovieID: $(element).attr('id').replace('movie_', ''),
              Rank: (i % 5) * 20 + index + 1,
              Year: year
            });
          });
          if(i === 1000 - 1) {
            await pWriteFile(`${outputPath}/movieList.json`, JSON.stringify(listJSON, null, 2));
          }
        });
      }, 1000 * i);
    })(i);
  }
})().catch(error => console.log(error));
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
Hyeon
  • 45
  • 5

2 Answers2

0

You can use the Ramda range() function to replace your loop.

https://ramdajs.com/docs/#range

R.range(0, 1000);

That will provide you with a collection of integers (your i) that you can work with (map() or whatever you need).

customcommander
  • 17,580
  • 5
  • 58
  • 84
Tom Faltesek
  • 2,768
  • 1
  • 19
  • 30
0

Steps:

1- Break your code in small functions
2- Stop using async await and use promise.then(otherFunction)
3- When using promise, you could create a sleep function like these: const sleep = (time) => new Promise(resolve => setTimeout(resolve, time));

Ex.:

const process = index => sleep(1000)
   .then(() => makeRequest(index))
   .then(processData);

R.range(0, 1000)
   .reduce(
       (prev, actual) => prev.then(() => process(actual),
       Promise.resolve()
   ) // Sequential
   .then(printResult);

R.range(0, 1000)
   .map(process) // Parallel
   .then(printResult);
Edgard Leal
  • 2,592
  • 26
  • 30