0

Simply, I want to store results in this array and want this result outside this.data function I want to pass these results as this.data and when I'm importing this function i use this.data

database = admin.database();

class PrivateSingleton {
  constructor() {
    let a = [];
    const keyref = database.ref("data");
    const finalfinaldata = keyref.get().then((data) => {
      const finalData = data.val();
      // console.log(finalData)
      return finalData;
    });

    this.data = finalfinaldata.then(function (result) {
      // console.log(result);
      a.push(result);
    });
    console.log(a);

    this.iv = "IVVV";
    this.key = "aaa";
  }
}

class Singleton {
  constructor() {
    throw new Error("Use Singleton.getInstance()");
  }

  static getInstance() {
    if (!Singleton.instance) {
      Singleton.instance = new PrivateSingleton();
    }
    return Singleton.instance;
  }
}

module.exports = Singleton;

OUTPUT

  : [] // Here I want my result in this array.
Smit Gajera
  • 1,001
  • 1
  • 8
  • 26
  • Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – jonrsharpe Oct 22 '21 at 07:26
  • Also you're exporting `Singleton` rather than `PrivateSingleton`. – Andy Oct 22 '21 at 07:30
  • [@Andy](https://stackoverflow.com/users/1377002/andy) Edited! Check now! – Smit Gajera Oct 22 '21 at 07:34
  • Post by @jonrsharpe should get you a long way. Also, keep in mind that your constructors can't be async, since constructors need to return the object it constructs (and not a Promise), so you need to call your async initialization logic via another method. Lastly, you could consider making Singleton's constructor private, that way you can catch violations at compile time rather than run time. – david-err Oct 22 '21 at 09:53

0 Answers0