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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:52:17+00:00 2026-06-11T21:52:17+00:00

I have this function: exports.readPartial = function(html, values, response) { fs.readFile(__dirname + /../html/partials/ +

  • 0

I have this function:

exports.readPartial = function(html, values, response) {
  fs.readFile(__dirname + "/../html/partials/" + html, function(error, file) {
  if(error) { this.loadError(response, error); }
  else {
   console.log("\t--> partial found: " + file);
   return file; // FILE VALUE
  }
  });
}

When that function is called it should return the value of “file”. However, when calling return file; im actually returning the value in the anonymous function I passed as an argument.
What is the right way to return this value on async programming with nodejs?
Using var that = this; ?

Got confused pretty confused with this style of programming.

  • 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-11T21:52:18+00:00Added an answer on June 11, 2026 at 9:52 pm

    Your readPartial functions relies on an asynchronous function readFile. So it gets asynchronous, too.

    Now you have several options on how to solve this problem:

    • Use the synchronous version of fs.readFile, which is fs.readFileSync (see http://nodejs.org/api/fs.html#fs_fs_readfilesync_filename_encoding). Then you are able to return the result as usual.
    • Change your function to use a callback, too.

    So, basically this means: Either both functions have to be asynchronous, or both need to be synchronous.

    Of these two options, I’d prefer the asynchronous one to use a callback as well: This is exactly what is Node.js’ core competency – asynchronous, non-blocking I/O. So if you do not have strong reasons against it, stick with the asynchronous version and use callbacks.

    Of course, you may be interested in what other options exist. So, here they are (ALTHOUGH I WOULD NOT RECOMMEND IT):

    • Call fs.readFile and “wait” for its callback to return. This basically means active waiting with a while(true) loop, and is definitely considered bad practice. Anyway, technically it is viable. For the rare case you really want to do this, or you are simply interested in how one might implement something like this, check out syncasync.js.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a file I have this code: module.exports.greet = function() {...} I want to
I have a function that exports values into a CSV file, but the comments
I have this function to create a request to another file to update the
I have this function in my js file. I need to give time for
I have this regexp var bodyRegExp = /function[\\s]+[(].+[)][\\s]+{(.+)}/; bodyRegExp.exec(module.exports = function () { /*
I have this nodeJS code. module.exports = { foo: function(req, res){ ... this.bar(); //
Say i have this code to separate routes in expressjs: module.exports = function(express,app,client) {
I have a one-function DLL that exports GetHash(). However, the source code for this
I have a function that exports a table into a CSV file, then I
I have this function // add history paths and save data function AddPath( strTag,

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.