Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
Store object in memory from an async method

I am trying to store an object in memory on Node.js. This object is created from an async method that is a call to an API. The thing is, this used to be a synchronous object that many parts of the application are importing, but now it is asynchronous. What I am trying to do is load this object in memory when I initialize the app so the files that are importing the synchronous object do not need to be refactored.

Example:

// Settings.js
class Settings {
  async build() {
    setTimeout(() => {
      return { app: 'settings' }
    }, 1000)
  }
}

//index.js - store object in memory when application is initialized
const intializeApp = async () => await new Settings().build()


//randomFile.js
import Settings from 'settings.js' //importing object {app: 'settings'}

How would I accomplish this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to store the settings and expose a way to access them. Since you're using classes, you can provide a get method:

// Settings.js
class Settings {
  async build() {
    return new Promise(resolve => {
      setTimeout(() => {
        this.settings = { app: 'settings' };
        resolve(); // so the async function can tell when the timeout is done
      }, 1000);
    }
  }
  get() {
    return this.settings;
  }
}

// Need to create one instance so it's shared between all the files that it's imported
export default new Settings();

//index.js - store object in memory when application is initialized
const intializeApp = async () => await Settings().build()


//randomFile.js
import Settings from 'settings.js' 
const settings = Settings.get();
console.log(settings);
// logs object {app: 'settings'}

And if you are fine with get being async could have it lazily initialize if there's no settings yet:

  async get() {
    if (!this.settings) await this.build();
    return this.settings;
  }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!