Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7896365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:52:39+00:00 2026-06-03T07:52:39+00:00

I’m new to Node.js and asynchronous programming in general, and I have a problem.

  • 0

I’m new to Node.js and asynchronous programming in general, and I have a problem. I need for continuous work server in a recursive loop, which steps would be implemented consistently, and each stage should consist of an asynchronous receive data over websotskets (this is already has understood), or go to next stage on the expiration of timer.
I am aware that is it not a trivial task, but how to implement it, what libraries can help?
I tried to understand the Step.js and Node.js events, this is what i need?

If I wrote all this in sync:

//CODE FOR STACKOVERFLOW
var previous_round = 'qwerty';// this is string, selected in previous cycle
var round_users = users;// 'users' is array of soceket.id all connected users and here a put them into new array 'round_users' with sockets for current cycle
io.sockets.emit('users_round', { number: round_users.length }); // send to users information about number of users in new round
for (var i = 0; i < round_users.length; i++) {
      io.sockets.socket(round_users[i]).emit('round', { text: previous_round });//for each socket in 'round_users' emit event to enter chat message
}                                                                               // and send selected in previous round message
var messages = []; // array of users messages
//now we must listen users events and start timer for next stage
  //listen users events
  for (var i = 0; i < round_users.length; i++) {
        io.sockets.socket(round_users[i]).on('message', function (data) {
          messages[messages.length] = data.text;//write text from users to array
          if (messages.length == round_users.length) { /* GO TO NEXT STAGE */ } // it's after previous operation in this function
        });
  } 
  //or set timeout
  setTimeout(/* GO TO NEXT STAGE */,15000);
for (var i = 0; i < round_users.length; i++) {
      io.sockets.socket(round_users[i]).emit('voting', { messages_array: messages });//for each socket in 'round_users' emit event to vote perfect chat message
}                                                                                    // and send messages, which they send to server
//i'm not quite sure about this decision of vote counting :-)
var votes = []; //array with users votes
for (var i = 0; i < messages.length; i++) {
      votes[i] = 0;
}   
//now we must listen users events and start timer
  //listen users events
  for (var i = 0; i < round_users.length; i++) {
        io.sockets.socket(round_users[i]).on('vote', function (data) {
          votes[data.number]++;//increment selected message
          if (votes.length == messages.length) { /* GO TO NEXT STAGE */ } // it's after previous operation in this function
        });
  } 
  //or set timeout
  setTimeout(/* GO TO NEXT STAGE */,10000);
var max_id = 0; //now select max number from 'votes' array
for (var i = 0; i < votes.length; i++) {
      if (votes[i]>votes[max_id]) {max_id = i;} //yet without the same values
}   
var previous_round = messages[max_id]; //set up string value of selected message
//restart cycle

this code on pastebin with syntax highlighting

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:52:40+00:00Added an answer on June 3, 2026 at 7:52 am

    Recursion in the most basic sense is just a function that calls itself over and again, but in this instance with node you may want to utilize process.nextTick() to allow other events to occur during this recursion.

    Here is a simple example of one way you might be able to go about it:

    function someFunction(args, callback) {
      // Check requirements
      if (typeof callback !== 'function') return console.log('Callback required');
      if (typeof args === 'undefined') return callback('Arguments required');
    
      // Set defaults
      var err = false;
      var result = false;
    
      // Do other stuff
      anotherFunctionWithAcallback(args, function(err, result) {
        if (err) return callback(err);
        // This way, the nextTick only occurs after processing completes
        return callback(err, result); 
      }
    }
    
    (function loop(stage) {
      // When the stage argument is not provided, default to stage 1
      stage = (typeof stage === 'undefined') ? 1 : stage;
      switch(stage) {
        case 1:
          /* stage 1 */
          someFunction(args, function(err, result){
            process.nextTick(loop(2));
          });
        break;
        case 2:
          /* stage 2 */
          someFunction(args, function(err, result){
            process.nextTick(loop(3));
          });
        break;
        case 3:
          /* stage 3 */
          someFunction(args, function(err, result){
            // No stage argument, restart at stage 1 by default
            process.nextTick(loop());
          });
        break;
      }
    })(); // Execute this function immediately
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.