I am having a route
app.get('/posts', function (req, res) {
res.json([
// data
]);
// what would happen to the code below
// Suppose it is processing a million records
processMillionRecords();
});
function processMillionRecords () {
// process million records
}
Once the response is sent, another function is called which is quite an expensive operation. What would happen if this continues? Are there any implications?
I know the ideal way of doing it is using background processes. So I replaced it with the below.
res.json([
// data
]);
var child = require('child_process');
child.fork('worker.js');
// worker.js
function processMillionRecords () {
// process million records
}
Which one is more preferred and in which cases?