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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:09:29+00:00 2026-06-02T01:09:29+00:00

I use nodejs code: var ret = firstfunction(); //this calls database and gets some

  • 0

I use nodejs code:

var ret = firstfunction();  //this calls database and gets some return value
var output = secondfunction(ret); // in this function we used ret as parameter 

Before firstfunction() completes , and before it gets the value “ret”, the second function executes with parameter “undefined” as “ret” is not yet available resulting in an error .

How can i execute second function, only after first function completes execution.

var uname = "sachin";
var noqq=UserModel.find({uname:uname},function(err,user){
if(!err){
    myid=user[0]._id;  //SAVING myid here , I AM USING THIS IN THE NEX FUNCTION
    return myid;
}else { 
     return null;
    }
});

Below function should execute only after above one completes, that is after getting “myid” only.

var ret=CollegeModel.findById(myid, function(err,colleges){
if(!err)
{
    res.send(questions);
}
else {
    res.send(err);
}
});

Please show me answer implementing my code.
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-06-02T01:09:31+00:00Added an answer on June 2, 2026 at 1:09 am

    With Node, you have to learn to do things asynchronously. You can use its many synchronous methods, but you lose out on the benefits of going fully non-blocking.

    So without knowing what’s in firstfunction, you probably have an asynchronous method in there. If so, firstfunction is returning before the asynchronous method is returned (internally, it’s probably not even been invoked). What you need to do is send your secondfunction into firstfunction(). The simplest way to do that is to pass secondfunction as an argument to firstfunction. Your code will look something like this:

    var firstfunction = function(myCallback) {
        var dbSuccessCallback = function(returedData) {
            // the asynchronous call has retured successfully with data
    
            myCallback(returnedData); 
        };
        callDatabase("SELECT a FROM b", dbSuccessCallback);
    }
    
    
    firstfunction(secondfunction);
    

    If you do event binding in HTML, you’re already probably very used to doing this:

    mydiv.addEventListener('click',function(){/* Do something */}, true);
    

    or another way:

    var myCallback = function(){
        /* Do something */
    };
    mydiv.addEventListener('click',myCallback, true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the nodejs connent modul code for sample. app.js follow this: var connect
I try to run this simple code in node.js: var YUI = require(yui3); YUI.use(function(Y)
I use this code to get checked date,but it's not get the int[] value.
I installed nodejs and socket.io, and when I run the demo code at http://socket.io/#how-to-use
I have a code like this: var a = selectBox.options[selectBox.selectedIndex]; If I execute: alert(a);
Following is my nodejs code var emitter = require('events'), eventEmitter = new emitter.EventEmitter(); eventEmitter.on('data',
I am new to nodejs. I want to use jsdom to parse some web
use this website a lot but first time posting. My program creates a number
Use case: I've just entered insert mode, and typed some text. Now I want
Let's say we have this code (forget about prototypes for a moment): function A(){

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.