1

I have a function in which I'd like several setInterval calls, however the code within in them is very similar.

Does anyone have any ideas on how I could better write this to avoid code duplication:

(async (): Promise<void> => {

    // 1 min interval check
    const oneMin = setInterval(async function () {
        Detail.find({ interval: 1 }, async function (err, toCheck) {
            // The Same Logic
        })
    }, minutesToMS(1));

    // 10 min interval check
    const tenMin = setInterval(async function () {
        Detail.find({ interval: 10 }, async function (err, toCheck) {
            // The Same Logic
        })
    }, minutesToMS(10));

})();

Any ideas appreciated.

Thanks.

1 Answer 1

2

Put this part:

async function (err, toCheck) {
  // The Same Logic
}

into a standalone variable first, so you can pass it to both .finds:

type CallbackType = Parameters<typeof Detail.find>[1];
const callback: CallbackType = async (err, toCheck) => {
    // logic
};
// 1 min interval check
setInterval(function () {
    Detail.find({ interval: 1 }, callback)
}, minutesToMS(1));

// 10 min interval check
setInterval(function () {
    Detail.find({ interval: 10 }, callback)
}, minutesToMS(10));

If you also want to avoid repeating the { interval: 1 } minutesToMS(1), then:

type CallbackType = Parameters<typeof Detail.find>[1];
const callback: CallbackType = async (err, toCheck) => {
    // logic
};
const makeInterval = (interval) => {
    setInterval(function () {
        Detail.find({ interval }, callback)
    }, minutesToMS(interval));
};
makeInterval(1);
makeInterval(10);
Sign up to request clarification or add additional context in comments.

8 Comments

Cool - that looks good. By the way having those multiple setIntervals in one function, I had to declare each setInterval into its own variable like const oneMin, const tenMin etc. It works, but of course my linter complains about the unused variable. Any ideas on that?
If you only need to initialize the interval, but not use the returned interval ID, then just don't assign the result to a variable
Also in the line type CallbackType = Parameters<typeof Detail.find>[1] what does that [1] refer to? DIdnt quite understand it
This is good, but you could go a step further and create: function createSetInterval(interval: number) { return setInterval(....); }
The question is tagged with Typescript, so I assume this is TS - if you want the callback function (its parameters and return type) to be typed correctly, TS needs to know its shape. This can be done by either listing all the possible types manually, or (a better option) by using Parameters to extract the type of the Details.find function, and [1] to extract the second parameter
|

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.