async-resource
This is a simple interface for initializing async resources. Send it an init function and it will return a function that calls back with your initialized resource. It ensures that the resource will only be initialized once.
Installation
$ npm install async-resource
Usage
With a class.
// DB client has a bunch of setup that requires async callsvar DBClient = Client;var getResource = getvar ensureResource = ensure{var self = this;// wrap the init method with getResourcethisensureConnected =};// simple methods that depend on a connected db.// a real wrapper would most likely have more complicted// methods.DbWrapperprototype {thisdb;};DbWrapperprototype {thisdb;};DbWrapperprototype {thisdb;};DbWrapperprototype {thisdb;};// wrap all the methods that depend on the connected DB;
With a function.
// DB client has a bunch of setup that requires async callsvar DBClient = Client;var getResource = getvar ensureFn = ensureFnvar db;var {DbClient;};var ensureInit = ;var {db;};// wrap selectselect = ;