Is providing a Promise as a module's export a valid pattern for asynch initialization in Node.js?

后端 未结 1 1093
谎友^
谎友^ 2020-12-29 04:33

I need to write some modules that load data one time and then provide an interface to that data. I\'d like to load the data asynchronously. My application already uses pro

相关标签:
1条回答
  • 2020-12-29 04:36

    This seems like a perfectly good interface for a module who's job is to do a one-time fetch of some data.

    The data is obtained async so a promise makes sense for that. The goal is to fetch the data just once and then let all places this module gets used just have access to that original data. A promise works great for that too because it's a one-shot device that remembers its state.

    Personally, I'm not sure why you need the getId() and getName() methods when you could just offer direct access to the properties, but either can work.

    A downside to this interface is that there is no means of requesting a fresh copy of the data (newly loaded from the DB).

    0 讨论(0)
提交回复
热议问题