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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:45:05+00:00 2026-06-05T00:45:05+00:00

I have a Node.js application with asynchronous code like this: var localvar1 = …;

  • 0

I have a Node.js application with asynchronous code like this:

var localvar1 = ...;
var localvar2 = ...;
var localvar3 = ...;
// do async database query
conn.query("select ...", function(err, res) {
    // fetch results asynchronously 
    res.fetchAll(function(err, rows) {
        if (rows.length == 0)   // no, data, fetch from external HTTP source
        {
            // fetch from Internet asynchronously 
            https.get(..., function() {
                // ok, got it, now save
                // I need values from original function here ***
                saveData(localvar1, localvar2, localvar3);
            });
        }
    });
);

What is the best way to pass values of localvar1-3 to innermost function?

If you suggest some library for this, please provide example code that would work with the code I wrote above.

  • 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-05T00:45:07+00:00Added an answer on June 5, 2026 at 12:45 am

    Thanks to closures, inner functions have access to local variables defined in outer functions or in the module itself, so your code as written will work if localvar1, et.al., don’t get changed while waiting for your https.get() callback to be called. If they do get changed, your callback will see their new values, not the values they had at the time of the request. If that’s a problem, create a new closure to “trap” them:

    https.get(..., (function(arg1, arg2, arg3) {
        return function() {
            saveData(arg1, arg2, arg3);
        }
    })(localvar1, localvar2, localvar3));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little Node.js application that I'd like to hit a remote API
I have an app.js node application. As this file is starting to grow, I
I have a symfony2 frontend application and I'd like to delegate all the asynchronous
I have a node application that is not a web application - it completes
I have a 3-node web farm being managed by IIS7 and Application Request Routing.
Does node.js (considering a web application) have any preferred UI framework? Or it's an
I use node.js and socket.io. I have an application that runs on the IP
i have this: //Node.h @interface Node: CCSprite{ BOOL wasTouched; } -(BOOL)getTouched; // some other
I have a mongoose model in my node.js application, representing invoices. I have figured
I have posted this to ServerFault, but the Node.js community seems tiny there, so

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.