Home > Enterprise >  Node Express. Continue function execution after res.render
Node Express. Continue function execution after res.render

Time:09-18

It is necessary to build the page and then continue the execution of the function, which takes a lot of time. But res.render waits for the function to execute no matter where it is called.
I want the page to start building without waiting for the data to be processed.
Here is my code:

        let promise = new Promise((resolve, reject) => {
            let wb = new Workbook();
            let ws = sheet_from_array_of_arrays(public_data); //May take up to 5 seconds
            wb.SheetNames.push(ws_name);
            wb.Sheets[ws_name] = ws;
            XLSX.writeFile(wb, '/tmp/'   name); //May take up to 10 seconds
            resolve('End of promise!!!');
        })
        res.render('ihelp/lists/person_selection_view_data', {
            data: public_data,
            name
        });
        promise.then(answer => { console.log(answer) });

How can i do this?

CodePudding user response:

The code that you want to run looks to be synchronous (I don't see any promises or callbacks related to it). If it takes multiple seconds to run, that will mean that it will block your entire app for that amount of time.

This means that asynchronous functions, like res.render(), will not complete until the processing is done. Even if you change the order:

res.render(...);
long_running_code();

Not only will this not make res.render() send back a response before long_running_code is started, it will also stop your app from responding to new incoming requests (and/or block any current requests) until it's done.

If you have CPU-intensive code that will block the event look, take a look at worker_threads, which can be used to offload CPU-intensive code to separate threads, and therefore keep your main JS thread free to handle the HTTP-part of your app.

CodePudding user response:

The res.render takes a 3rd argument for a callback. If supplied, Express will not send the rendered html string automatically. Any extended process can be executed after that.

res.render('ihelp/lists/person_selection_view_data', {data: public_data, name}, function (err, html) {

  res.send(html);    

  let wb = new Workbook();
  let ws = sheet_from_array_of_arrays(public_data); //May take up to 5 seconds
  wb.SheetNames.push(ws_name);
  wb.Sheets[ws_name] = ws;
  XLSX.writeFile(wb, '/tmp/'   name); //May take up to 10 seconds   


})

CodePudding user response:

The executor function (inside the Promise constructor) is executed synchronously, and if this takes too long, you can move it into another Promise:

res.render('ihelp/lists/person_selection_view_data', {
  data: public_data,
  name
});
Promise.resolve().then(function() {
  new Promise((resolve, reject) => {
    // your executor code
  }).then(answer => { console.log(answer) });
});
  • Related