0

I'm using node-worker-threads-pool to process a daily function on all the documents from one collection, for that I'm using a for loop to send the id of each document to the pool, I'm trying to get the time that it takes to process the whole process but it consoles before the execution of all the id's, so I have two questions, is better to run the for loop inside of the worker file so the pool.exec() function is just called once?, and how can I log the time with the actual code?. Here is my code:

Client.find({}, {_id: 1}).then(clients => {
        console.time('clientDayChange');

        const pool = new StaticPool({
            size: 8,
            task: "./worker.js"
        });
        
        for(let client of clients){
            let id = JSON.stringify(client._id);
            pool.exec(id);
        }
        
        console.timeEnd('clientDayChange');
   })
2
  • Hopefully you can use await inside an async function as then is always such a pain. Commented Sep 25, 2023 at 17:03
  • Presumably pool.exec returns a promise, so you should do something with it rather than discard the result. Also, "Is it better to..." / "what's the best way to..." is a bad way to phrase a question, because "better" and "best" are not always as objective as you think they are without clear criteria upon which to judge it. Otherwise, such questions are sometimes vague and elicit opinions. You can just try both approaches and tell us which is better for you. "How can I..." is a much better phrasing for stack overflow questions as it forces you to articulate your objectives. Commented Sep 25, 2023 at 17:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.