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

  • Home
  • SEARCH
  • 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 8342761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:51:41+00:00 2026-06-09T05:51:41+00:00

How can I best handle a situation like the following? I have a constructor

  • 0

How can I best handle a situation like the following?

I have a constructor that takes a while to complete.

var Element = function Element(name){
   this.name = name;
   this.nucleus = {};

   this.load_nucleus(name); // This might take a second.
}

var oxygen = new Element('oxygen');
console.log(oxygen.nucleus); // Returns {}, because load_nucleus hasn't finished.

I see three options, each of which seem out of the ordinary.

One, add a callback to the constructor.

var Element = function Element(name, fn){
   this.name = name;
   this.nucleus = {};

   this.load_nucleus(name, function(){
      fn(); // Now continue.
   });
}

Element.prototype.load_nucleus(name, fn){
   fs.readFile(name+'.json', function(err, data) {
      this.nucleus = JSON.parse(data); 
      fn();
   });
}

var oxygen = new Element('oxygen', function(){  
   console.log(oxygen.nucleus);
});

Two, use EventEmitter to emit a ‘loaded’ event.

var Element = function Element(name){
   this.name = name;
   this.nucleus = {};

   this.load_nucleus(name); // This might take a second.
}

Element.prototype.load_nucleus(name){
   var self = this;
   fs.readFile(name+'.json', function(err, data) {
      self.nucleus = JSON.parse(data); 
      self.emit('loaded');
   });
}

util.inherits(Element, events.EventEmitter);

var oxygen = new Element('oxygen');
oxygen.once('loaded', function(){
   console.log(this.nucleus);
});

Or three, block the constructor.

var Element = function Element(name){
   this.name = name;
   this.nucleus = {};

   this.load_nucleus(name); // This might take a second.
}

Element.prototype.load_nucleus(name, fn){
   this.nucleus = JSON.parse(fs.readFileSync(name+'.json'));
}

var oxygen = new Element('oxygen');
console.log(oxygen.nucleus)

But I haven’t seen any of this done before.

What other options do I have?

  • 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-09T05:51:43+00:00Added an answer on June 9, 2026 at 5:51 am

    Given the necessity to avoid blocking in Node, the use of events or callbacks isn’t so strange(1).

    With a slight edit of Two, you could merge it with One:

    var Element = function Element(name, fn){
        this.name = name;
        this.nucleus = {};
    
        if (fn) this.on('loaded', fn);
    
        this.load_nucleus(name); // This might take a second.
    }
    
    ...
    

    Though, like the fs.readFile in your example, the core Node APIs (at least) often follow the pattern of static functions that expose the instance when the data is ready:

    var Element = function Element(name, nucleus) {
        this.name = name;
        this.nucleus = nucleus;
    };
    
    Element.create = function (name, fn) {
        fs.readFile(name+'.json', function(err, data) {
            var nucleus = err ? null : JSON.parse(data);
            fn(err, new Element(name, nucleus));
        });
    };
    
    Element.create('oxygen', function (err, elem) {
        if (!err) {
            console.log(elem.name, elem.nucleus);
        }
    });
    

    (1) It shouldn’t take very long to read a JSON file. If it is, perhaps a change in storage system is in order for the data.

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

Sidebar

Related Questions

Is there a canonical best-way-to-do-this for the following situation? I have a block of
I'll try to explain this the best I can. I have a drop down
I have a file server and wanting to optimize it the best I can,
I have a situation where I currently have a huge spreadsheet that is used
Hi I Have a situation like this. I have to populate 2 labels and
I have a little dilemma that maybe you can help me sort out. I've
I have come across a situation today which has me wondering about best practices.
I'm trying to figure out the best practice for dealing with the following situation:
How can one best test a controller action which receives a file upload using
How can I best implement a system for send keystrokes/commands via an iPhone and/or

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.