0

First I write summarized pseudo code.

const ExcelSheet = new ExcelSheet() // excel.js library

const usersWithPlayedGames  = await findAllUsers({ include: GameTable });

for (let i = 0; i < usersWithPlayedGames.length; i++) {
    // Write some user data on Excel.
    ...
    ...
    for (let j = 0; j < usersWithPlayedGames[i].length; j++) {
        // Write some user's game on Excel
        ...
        ...
        for (let k = 0; k < usersWithPlayedGames[i][j].length; k++) {
            // Write some users's game's company data on Excel
            ...
            ...
        }
    }
}

res.send(ExcelSheet.toFile());

The actual code is pretty long.

And it take client req => res time almost 15sec.

I know my problem solution is not good.

I can do refactoring with this code.

But the real problem is it blocks another client request.

I search on google and find several solutions.

  1. Node.js child process spawn.
  2. Make with a callback function(I don't know exactly how to do).
  3. Write more than a good algorithm.

What's the concept I'm missing from Node.js?

Please help me with a small favor.

Thank you.

3
  • Write some user data on Excel., Write some user's game on Excel and Write some users's game's company data on Excel .... is there an asynchronous way of doing these? Since you haven't shown this code its hard to guess ... but that would solve the blocks another client request issue Commented Oct 4, 2019 at 1:36
  • @Bravo Thank you but I can't find with excel.js library async method. Commented Oct 4, 2019 at 1:47
  • There are some, but perhaps the refactoring from your current code is going to be too difficult. Commented Oct 4, 2019 at 1:51

1 Answer 1

1

You're porbably better off running this in a child process or somewhere else outside your web request thread, but if that's not an option, you could break the task up using something like setImmediate

const ExcelSheet = new ExcelSheet() // excel.js library

const usersWithPlayedGames  = await findAllUsers({ include: GameTable });

const excelLoop = (index) => {

  for (let j = 0; j < usersWithPlayedGames[index].length; j++) {
      // Write some user's game on Excel
      ...
      ...
      for (let k = 0; k < usersWithPlayedGames[index][j].length; k++) {
          // Write some users's game's company data on Excel
          ...
          ...
      }
  }

  if (index < usersWithPlayedGames.length) {
    setImmediate(() => excelLoop(index + 1))
  }
  else {
    res.send(ExcelSheet.toFile());
  }
};

excelLoop(0);

Documentation for setImmediate

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I will follow your link

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.