Is importing module into the loop a bad practice?

Asked

Viewed 49 times

0

Good morning guys, I have a javascript module and I import it at the beginning of my main/main code and I run a foreach, still in this main code has loop that calls the methods of the module:

results.forEach(function(result) {
     for(const item of items .......){
        exampleModule.metodo(result, item)
     }
});

So far so good, the problem is that the values of the module are being mixed, the module is not dynamic according to item Y of the result X, it kind of "mess" all over...

I thought of a way to solve, I don’t know if it’s best practice, declare const exampleModule = require('./modules/example.js') within an object and give that object a reference, such as the item ID, since each ID will be unique:

let objetos = {}
results.forEach(function(result) {
     for(const item of items .......){
        objetos[item.id] = require('./modules/example.js')
        objetos[item.id].metodo(result, item)
     }
});

In short, I want each loop item to create an instance of the module without them being interfered with, if I instantiate the module in position item 0, everything in the module should only be used by that item 0, if it is item 1 the same thing, 1 n can change values of the module that is being used by position 0 and so on, is it possible? Can someone please help me?

  • "the module is not dynamic according to the Y item of the X result" This has become quite confusing and gives the impression that what you are doing can be a XY problem. Why do you need a module to be "dynamic"? And what would those Y and X values be?

  • Describing the general problem you will get only a general answer. Example: "How to build a house?" , "Use blocks and cement, build solid walls". Instead ask something specific and responsive in a useful way: "How to lift a wall using this type of block with such a slope and such height safely?" , answer: "Position the blocks in such format, run this block placement algorithm, do not use this tool because there is such a risk, here is an example running from a wall ready for you to see how it does [link]". See? Too many wide questions don’t help.

  • I’m sorry, I didn’t know how to express myself in the best way. Is that this problem never happened to me, X and Y are loop positions, the item and the result are objects come from the API and are used in the modules

1 answer

1


You should import the module only once. The preference is that it be at the beginning of your file. If you use incremental values within your function, you can use a class to create a new instance with each interaction of loop:

class Exemplo {
  constructor() {
    this.valor = 0;
  }

  funcao(resultado, item) {
    this.valor = resultado + item;
    return this.valor;
  }
}

module.exports = Exemplo;

And would use the above module as follows:

const Exemplo = require('./Exemplo');

// ...
const exemplo = new Exemplo();
for (...) {
   console.log(exemplo.funcao(resultado, item));
}

It may be that your problem is also in relation to asymchronism, but with the information given here it is not possible to suggest a solution.

  • I think you understand my problem, but you see I have a foreach? This foreach calls the results to run in "parallel", if I instantiate the class out of the loop it will end up mixing in the same way

  • @viniciussvl has 2 loops in your question. You can instantiate within the forEach and before the for

  • 1

    Oh yes it is that I saw your code not compared to mine, wonder then Sorach, this can solve the problem for good, thank you for helping me!

  • So @Sorack I did it this way but it doesn’t seem to work, I saw that in your method "function" you pass the parameters, but I’m passing the parameters in the constructor, will it be that?

  • 1

    @viniciussvl do not know, your example is not testable, it is difficult to opine

Browser other questions tagged

You are not signed in. Login or sign up in order to post.