最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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 promises. Is providing a promise as the result of requiring a module a valid pattern/idiom?

Example Module:

var DB = require('promise-based-db-module');

module.exports =
  DB.fetch('foo')
  .then(function(foo){
    return {
        getId: function(){return foo.id;},
        getName: function(){return foo.name;}
    };
  });

Example Usage:

require('./myPromiseModule')
.then(function(dataInterface){
  // Use the data
});

UPDATE:

I've used this for a while now and it works great. One thing I've learned, and it's hinted at in the accepted answer, is that it is good to cache the promise itself, and whenever you want to access the data use then. The first time the data is accessed, the code will wait until the promise is resolved. Subsequent usage of then will return the data immediately. e.g.

var cachedPromise = require('./myPromiseModule');
cachedPromise.then(function(dataInterface){
  // Use the data
});
...
cachedPromise.then(function(dataInterface){
  // Use the data again somewhere else.
});

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 promises. Is providing a promise as the result of requiring a module a valid pattern/idiom?

Example Module:

var DB = require('promise-based-db-module');

module.exports =
  DB.fetch('foo')
  .then(function(foo){
    return {
        getId: function(){return foo.id;},
        getName: function(){return foo.name;}
    };
  });

Example Usage:

require('./myPromiseModule')
.then(function(dataInterface){
  // Use the data
});

UPDATE:

I've used this for a while now and it works great. One thing I've learned, and it's hinted at in the accepted answer, is that it is good to cache the promise itself, and whenever you want to access the data use then. The first time the data is accessed, the code will wait until the promise is resolved. Subsequent usage of then will return the data immediately. e.g.

var cachedPromise = require('./myPromiseModule');
cachedPromise.then(function(dataInterface){
  // Use the data
});
...
cachedPromise.then(function(dataInterface){
  // Use the data again somewhere else.
});
Share Improve this question edited Apr 4, 2018 at 20:59 Tony asked Sep 25, 2015 at 3:50 TonyTony 9921 gold badge10 silver badges22 bronze badges 3
  • 1 Looks like it would work well to me. – jfriend00 Commented Sep 25, 2015 at 3:52
  • 1 Yes, seems perfectly fine. If you are using ES6 modules, you might even be able to use a promise-aware module loader, and import "synchronously". – Bergi Commented Sep 26, 2015 at 15:13
  • Did the answer below solve your issue? If so, please mark the best answer as the accepted answer by checking the green checkmark to the left of that answer to indicate to the community that your question has been answered and then both you and the person who provided the answer will earn some reputation points that can lead to more privileges here on StackOverflow. – jfriend00 Commented Sep 27, 2015 at 17:23
Add a comment  | 

1 Answer 1

Reset to default 12

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)

  1. 暂无评论