N
N
Nwton2016-05-12 16:19:44
recursion
Nwton, 2016-05-12 16:19:44

How to create an eternal process in node.js?

The task is as follows: every second a random number is generated on the server and stored in the database. Constantly, even if there is not a single client.
What is the best way to implement this? The first thing that comes to mind is to write a simple generate+save function. into the database and at the end of execution with a delay of 1 second, run it again, recurse.
Is this an adequate solution, even if it turns out to dodge and create a recursion? Or is it possible in some other way? Which way to look?
I am new to node.js.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Wolf, 2016-05-12
@Nwton

setInterval(function() {
  db.write({  }); // пишем в базу каждую секунду
}, 1000);

D
Dmitry Belyaev, 2016-05-12
@bingo347

As for setInterval, Alexander Wolf
already suggested. On my own, I’ll add another tip to get acquainted with such a utility as pm2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question