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 7615369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:37:51+00:00 2026-05-31T02:37:51+00:00

I have code like common.findOne(‘list’, {‘listId’: parseInt(request.params. istId)}, function(err, result){ if(err) { console.log(err); }

  • 0

I have code like

common.findOne('list', {'listId': parseInt(request.params. istId)}, function(err, result){       
  if(err) {
    console.log(err);
  }
  else {
    var tArr = new Array();               
    if(result.tasks) {
      var tasks = result.tasks;
      for(var i in tasks) {
        console.log(tasks[i]);
        common.findOne('tasks', {'taskId':parseInt(tasks[i])}, function(err,res){
          tArr[i]  = res;       
          console.log(res);                     
        });                       
      }
      console.log(tArr);
    }               
    return response.send(result); 
  }
});

It is not executed sequentially in node.js so I get an empty array at the end of execution. Problem is it will first execute console.log(tArr); and then execute

common.findOne('tasks',{'taskId':parseInt(tasks[i])},function(err,res){
      tArr[i]  = res;       
      console.log(res);                                         
});                       

Is there any mistake in my code or any other way for doing this.
Thanks!

  • 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-05-31T02:37:52+00:00Added an answer on May 31, 2026 at 2:37 am

    As you are probably aware, things run asynchronously in node.js. So when you need to get things to run in a certain order you need to make use of a control library or basically implement it yourself.

    I highly suggest you take a look at async, as it will easily allow you to do something like this:

    var async = require('async');
    
    // ..
    
    if(result.tasks) {
      async.forEach(result.tasks, processEachTask, afterAllTasks);
    
      function processEachTask(task, callback) {
        console.log(task);
        common.findOne('tasks', {'taskId':parseInt(task)}, function(err,res) {
          tArr.push(res); // NOTE: Assuming order does not matter here
          console.log(res);
          callback(err);
        });
      }
    
      function afterAllTasks(err) {
        console.log(tArr);
      }
    }
    

    The main things to see here is that processEachTask gets called with each task, in parallel, so the order is not guaranteed. To mark that the task has been processed, you will call callback in the anonymous function from findOne. This allows you to do more async work in processEachTask but still manage to signify when it is done. When every task is done, it will then call afterAllTasks.

    Take a look at async to see all the helper functions that it provides, it is very useful!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code like this var MyObj = { f1 : function(o){ o.onmousedown =
I have several views that have common code I'd like to abstract into a
I have code like this: public bool Set(IEnumerable<WhiteForest.Common.Entities.Projections.RequestProjection> requests) { var documentSession = _documentStore.OpenSession();
Possible Duplicate: Are do-while-false loops common? Is there a reason to have code like:
I have code that looks like this: function foobar(array& $objects, $con = null) {
As you all know, it's extremely common to have code like this: <a href='#'
I have code like this: var newMsg = new Msg { Var1 = var1,
I have code like this: template <typename T, typename U> struct MyStruct { T
I have code like this to move the player in my game left, right,
I have code like: @notifications = Notification.find_all_by_user_id(@user.id, :order=>'deliver_by DESC', :conditions=>deliver_by >= '#{Date.today.to_s(:db)}') logger.info @upcoming_reminders[0].inspect

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.