Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. }); 
like image 514
Tony Avatar asked Sep 25 '15 03:09

Tony


People also ask

Can I use promise in node JS?

In Node. js, we can use the util. promisify() utility module to easily transform a standard function that receives a callback into a function that returns a promise.

How does a promise work in node?

A Promise in Node means an action which will either be completed or rejected. In case of completion, the promise is kept and otherwise, the promise is broken. So as the word suggests either the promise is kept or it is broken. And unlike callbacks, promises can be chained.

Is promise all sequential or parallel?

Final Thoughts: Parallel ProcessingOften Promise. all() is thought of as running in parallel, but this isn't the case. Parallel means that you do many things at the same time on multiple threads. However, Javascript is single threaded with one call stack and one memory heap.

How do you handle multiple asynchronous calls in node JS?

In order to run multiple async/await calls in parallel, all we need to do is add the calls to an array, and then pass that array as an argument to Promise. all() . Promise. all() will wait for all the provided async calls to be resolved before it carries on(see Conclusion for caveat).


1 Answers

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).

like image 173
jfriend00 Avatar answered Sep 27 '22 23:09

jfriend00