Incorporating code execution during promise completion



I'm currently working on an express application that involves a generator function which takes approximately 5 minutes to process a large amount of data. Unfortunately, I am unable to optimize this function any further.
Express has a built-in timeout of 2 minutes, and I do not want to change this setting just for this specific function. I thought that by periodically calling res.write(), the 2-minute rule might no longer apply.

My query is as follows:
How can I implement a res.write('Something') every X seconds while waiting for the other function to complete?

I would like it to perform something similar to the code snippet below, in case you understand my intentions.

    function main() {
      co(function* () {
        const something = yield promise(); // function with long processing time
        const doWhileWaiting = setTimeout(() => {
          if (!something) {
            // Display this message while waiting for the value of 'something'
            console.log('Waiting for something ... ');
          } else {
            console.log(something);
            clearInterval(doWhileWaiting);
          }
        }, 5000);
      });
    }

Answer №1

Implementing this is actually quite simple:

const special = Symbol();
const pause = ms => new Promise(resolve => setTimeout(() => resolve(special), ms));

async waitFor(promise) {
    while(true) {
        const competitor = await Promise.race(promise, pause(1));
        if(competitor !== special) return; // the promise we're waiting for has resolved!
        await pause(100);
   }
}

Once you reach this point, you can include an action to execute when the task is not yet completed.

async waitFor(promise, performAction) {
    while(true) {
        const competitor = await Promise.race(promise, pause(1));
        if(competitor !== special) return; // our target promise has succeeded!
        await performAction(); // allows passing a function as an action
        await pause(100);
   }
}

// will append "..." to `result` every 100 ms until `yourPromise` is resolved.
waitFor(yourPromise, () => result.append("...")); 

Keep in mind that it's advisable to place additional tasks in a queue and utilize a separate process to handle them. Node may not be optimized for such heavy workloads. I suggest exploring other solutions mentioned in another answer.

Answer №2

It's not recommended to have a heavy, blocking function directly in your API. It would be more efficient to offload this type of computation to another process since Node is single-threaded and running such a function could block other computations. While it is possible to add timeouts to your computing functions to run them within the same thread/process, it may not be the best practice. Without knowing the specifics of your function, it's difficult to provide a definitive answer on what's optimal for your situation.

If your function only needs to be executed once a day, consider setting up a cronjob to trigger another process. If you need communication between your API process and the secondary process, child processes in Node.js can be useful. https://nodejs.org/api/child_process.html

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Obtain the content of a clicked item on the following page using NextJs

I am currently working on a nextjs app that displays a list of 10 movies on the homepage, each with a Button / Link that leads to a specific page for that movie where all its content is shown. Initially, I tried adding the movie id to the Link like this: ...

Having trouble getting the mock module to work with mockImplementation

I have been facing a challenge in properly testing this File. Some tests require mocking the entire module, while others only need specific methods mocked. I have tried various combinations, but currently, for one specific test below, I am attempting the f ...

How can you fetch data from a PHP file using AJAX before performing a header redirect?

I am currently in the process of adding more dynamism to my website. I have developed a forum from scratch and now I am integrating JavaScript into it. All the PHP backend work is complete. My next goal is to enable user login without having to refresh the ...

What is the best way to format and return a result object list in JavaScript or Angular?

I have a question regarding the use of for loops in JavaScript or utilizing Angular to output the resulting object list. Here is an example of an object list: var alist = []; alist = [ { 'code': 1000, 'type': 'C' ...

What is the best way to incorporate my Switch component into my Table component as a distinct column, while also maintaining separate states for each component?

My journey into learning React.js has been exciting so far. However, I am facing some confusion when it comes to stateful components. Currently, I am utilizing Bootstrap tables for creating a table and successfully fetching data using GET requests. Additio ...

The absence of the 'Access-Control-Allow-Origin' header is detected in the requested resource by Keycloak

We are currently experiencing an issue with accessing our nodejs app from Chrome, which has Keycloak configured. Keycloak version: 21.0.1 When trying to access http://localhost:3101/graphql from Chrome, we encountered the following error in the browser c ...

Ways to combine duplicate entries within a column using Ruby on Rails?

I need some help with a filtering issue related to sign names. I am trying to merge the content together if there is more than one name of the sign. You can refer to the image attached for better clarity, where I have two server names but I only want to di ...

Grab the code snippet from JSFiddle

I apologize for the seemingly simple question, but I have been struggling with it. I tried looking at similar questions, but I couldn't find a solution. Despite copying the code from http://jsfiddle.net/sB49B/21/, I can't seem to get it to work ...

Why isn't my React image updating its source right away? What are some solutions to this issue?

I currently have a basic <img> tag with a specified src attribute. <img src={src} /> When I update the src attribute from, let's say /1.jpg to /2.jpg, there is a delay in loading the new image. React still displays the old image (/1.jpg) ...

Displaying a component after retrieving a value from AsyncStorage in a React Native application

I have developed a React Component that saves user settings in the AsyncStorage and retrieves them upon loading. The functionality of storing and retrieving data is working fine, but I am facing an issue where the component renders before the values are ...

Cloudflare SSL Error 522 Express: Troubleshooting Tips for Res

After setting up my express project using express-generator, I decided to make it work with a cloudflare SSL Certificate for secure browsing over https. My express app is running on port 443. Despite my efforts, when I try to access the domain, I encount ...

Access the value of a variable from a window resizing event and utilize it in a different

I have a carousel that I'm working with and am trying to figure out how to announce the number of currently visible slides when the "next" button is clicked. While I can see that on resize, the correct number of slides is being logged, I am strugglin ...

What is the best way to retrieve the URL query parameters in a Node.js environment?

How can I retrieve URL query parameters in Node.js using TypeScript? Below is my attempted code: /** * My Server app */ import * as Http from "http"; import * as url from "url"; import { HttpServer } from "./HttpServer"; import { TaxCalculator } from ". ...

My selection of jQuery multiselect is experiencing issues with enabling disabled options

Incorporating the chosen jQuery plugin into my project has presented me with a challenge. The issue at hand is listed below. I have implemented a dropdown menu that includes both continents and countries in the same list. The specific scenario I am encou ...

Having trouble processing the Firebase snapshot with Node.js

I have a question regarding a snapshot; ref.orderByChild("index").equalTo(currentIndex).once("value", function(snapshot) {}) After printing the snapshot with ; console.log(snapshot.val()); This is the output that gets printed; {'-LBHEpgffPTQnxWIT ...

Incorporate validation features within a jQuery method

I am struggling with some HTML and jQuery code that generates links based on user input. HTML <input type="text" id="text" placeholder="Enter text" autofocus /> <a id="link1" class="btn btn-info" href="#" target="_blank"> Search 1 </a> ...

What is the best way to achieve this in Node.js/Express using Redis? Essentially, it involves saving a callback value to a variable

Here is the data structure I have set up in redis. I am looking to retrieve all values from the list and their corresponding information from other sets. Data structure : lpush mylist test1 lpush mylist test2 lpush mylist test3 set test1 "test1 value1" ...

A demonstration of ExpressJS middleware

Hey there, I've got some code in my app.js file that looks like this: app.use('/someurl', require('./middleware/somemodule')); -change app.use to app.all The module itself is structured as follows: if(process.env.BLALAL === un ...

Is there a way to retrieve the id attribute of an option element from the source component?

When a user makes a selection, I am trying to access the id attribute of the HTMLOptionElement. However, it always returns 0 instead of the actual id I passed (such as 1 or 2) from the select tag: <div class="col-8"> <select (cha ...

What is the best way to retrieve the current directory in npm scripts?

Consider a scenario where the package.json for my projects is located as follows: project |- package.json Now, imagine that I run an npm script from a different location within the project structure: project |- package.json |- some |- nested |- ...