Pierre Olivier Tran
Pierre Olivier Tran

Reputation: 1875

Best way to loop node.js script on large array

I'm fetching an array of objects from a DB, and using their data in a heavy method, to create some SVG files.

MongoClient.connect(MONGO)
  .then((database) => {
    data = database
    db = database.db('apidatabase')
    return db.collection('Products')
      .find({
        owner: 'pingu',
      })
      .toArray()
  })
  .then((products) => {
    products.forEach((product, index) => {
      createFiles(product) // the heavy method
    })
  })
  .catch((error) => {
    console.log({ error })
    data.close()
  })

If I start my script for all the objects (I have more than 6000 of them), the scripts freezes, and nothing gets done. I have to slice my array, and get through them all by slices of 20 objects.

Is there a way to properly iterate though these objects, so that the script runs smoothly for all 6000 objects ?

Upvotes: 1

Views: 613

Answers (1)

imkrieger
imkrieger

Reputation: 102

You should fork createFiles function in Node.JS and process it. Refer documentation to understand, how you can fork and retrieve entire response back to your function.

Upvotes: 1

Related Questions